直接上代码咯。(windows7 x64, python2.7)#encoding=utf-8
#author: walker
#date: 2014-11-18
#function: 示例python2读取utf8文件(含中文等特殊字符)
import sys
reload(sys)
sys.setdefaultencoding('utf8')
file = open('data.txt', 'r')
print('*******************************')
for line in file:
print(line.strip().encode('gb18030'))
print('-------------------------------')
print(line.strip())
print('*******************************')
file.close()
运行结果:
小知识:gb2312 < gbk < gb18030 (参考一,二)
*** walker * 2014-11-18 ***