python (11)文件的读写 按行读文件

读文件:

读取文件
f = open('\info.txt')
fil = f.read()
f.close()

 按行读文件:

f = open("info.txt")
while 1:
   line = f.readline()
   line=line.strip('\n') # 去掉换行符
   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")

 

 

posted on 2016-03-28 17:06  细雨微光  阅读(677)  评论(0编辑  收藏  举报