直接上代码咯。(windows7 x64, python2.7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#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 snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1577735如需转载请自行联系原作者
RQSLT