方法一:使用codecs
import codecs
f = codecs.open('nlpir/Readme.txt','r','GBK')
line = f.readline()
while line:
print line,
line = f.readline()
f.close()
上面的方法很慢,可以直接读取整个文件
codecs.open('nlpir/Readme.txt','r','GBK').read()
还有这样读的readlines()
方法二:
#读取文档
def read_file():
path1='K:\\SogouC.reduced\\Reduced\\C000008\\10.txt'
f = open(path1,'r+')
file_list = f.read().decode("gbk")
print file_list
f.close()
#逐行读取文档
def read_file_line():
path1='K:\\SogouC.reduced\\Reduced\\C000008\\10.txt'
f = open(path1,'r+')
line = f.readline()
while line:
print line.decode('gbk')
line = f.readline()
f.close()