直接上代码咯。(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 ***
本文出自 “walker的流水账” 博客,请务必保留此出处http://walkerqt.blog.51cto.com/1310630/1577735
原文地址:http://walkerqt.blog.51cto.com/1310630/1577735