读文件:
读取文件 f = open('info.txt') fil = f.read() f.close()
按行读文件:
f = open("info.txt") while 1: line = f.readline() line=line.strip(' ') # 去掉换行符 if not line: break print line f.close()
读取文件报错:
f1=open("fenci_result.txt",'r')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 2892: invalid continuation byte
解决办法:
import codecs f1=codecs.open("fenci_result.txt",'r',encoding = "ISO-8859-1")