Python--读取含有中文的TXT

方法一:

import codecs

with codecs.open('somefile.txt', 'r', 'GBK') as f:
data = f.read()
print data


方法二:

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

fin = open('somefile.txt', 'r')
for eachLine in fin:
line = eachLine.strip().decode('gbk', 'utf-8')
print line


你可能感兴趣的:(Python--读取含有中文的TXT)