注:本章仅介绍python自带function,一般数据分析多用pandas包
infile = open("input.txt", "r")
#打开文件input.txt中读取数据,r代表read;读取二进制文件“rb”
outfile = open("output.txt", "w")
#打开文件output.txt准备输入数据,w代表write;二进制“wb”
infile.close()
# 关闭文件;执行完文件操作之后最好关一下,没试过python不关会怎样;
# C不关闭的话会死很惨是真的
outfile.close()
file_obj = open(filename, mode = 'r', buffering = -1)
# buffering是可选参数,默认值为-1,0代表不缓冲
# 1或大于1的值表示缓冲1行或支付那个缓冲区大小
f.read() # f:对象,read():方法(参数)
file_obj.seek(offset, whence = 0)
# offset:偏移量,0表示文件头部,1表示当前位置,2表示文件尾部偏移;whence:offset个字节
# f.seek(0,0)表示从头开始,f.seek(1,50)表示从指针往后移动50个字节
如果没有设置路径,那么要打开的文件一定要放在python的安装目录下。如果文件在其他目录中,比如在D盘下,infile = open("D:\\\\input.txt", "r")在前面加上路径就OK了。生成文件也一样。
另外,python路径设置要使用到import os。
os.getcwd() # 查看当前工作目录
os.chdir("D:\\\\ ") # 改变目录
while循环版:
line = infile.readline()
while line != "" :
print(line)
line = infile.readline()
2. for循环版:
for line in infile :
print(line)
注意:读入的数据是字符换格式。另外用readline读入数据的时候,会把每一行包括回车在内的全部内容都读到line中。如果要去掉回车或者去掉你不需要的部分,则需要用到以下方法:
| Method | Returns |
|---|---|
| s.lstrip() | |
| s.lstrip(chars) | A new version of s in which white space (blanks, tabs, and newlines) is removed from the left (the front) of s. If provided, characters in the string chars are removed instead of white space. |
| s.rstrip() | |
| s.rstrip(chars) | Same as lstrip except characters are removed from the right (the end) of s. |
| s.strip() | |
| s.strip(chars) | Similar to lstrip and rstrip, except characters are removed from the front and end of s. |
e.g. 例子的Results中的空格以下划线_代替
| Statement | Result | Comment |
|---|---|---|
| string = "James\n" | ||
| result = string.rstrip() | J a m e s | The newline character is stripped from the end of the string. |
| string = "James \n" | ||
| result = string.rstrip() | J a m e s | Blank spaces are also stripped from the end of the string. |
| string = "James \n" | ||
| result = string.rstrip("\n") | J a m e s _ | Only the newline character is stripped. |
| name = " Mary " | ||
| result = name.strip() | M a r y | The blank spaces are stripped from the front and end of the string. |
| name = " Mary " | ||
| result = name.lstrip() | M a r y _ | The blank spaces are only stripped from the front of the string. |
所以,完整版的读行代码是:
for line in infile :
line = line.strip()
# 此处应该有处理line的代码,比如outfile.write(line);不然再读line就被覆盖掉了
生成文件