基于python2如何将txt内utf-8转换成中文,python2utf-8,在python2中如何将
基于python2如何将txt内utf-8转换成中文,python2utf-8,在python2中如何将
在python2中如何将txt中的utf-8转换成中文
txt中的内容是这样的:
('\xef\xbc\x8c\n', 30)
('\xe7\x9a\x84\n', 12)
('\xef\xbc\x81\n', 11)
('\xe8\xbf\x98\n', 7)
('\xe5\xbe\x88\n', 6)
('\xe6\xb2\xa1\n', 6)
('\xe5\xae\x89\xe8\xa3\x85\n', 5)
('\xe4\xbd\xbf\xe7\x94\xa8\n', 3)
'\xe5\x90\xa6\xe5\x90\xa6'.decode('utf-8')#否否
test.txt
('\xef\xbc\x8c\n', 30)('\xe7\x9a\x84\n', 12)('\xef\xbc\x81\n', 11)('\xe8\xbf\x98\n', 7)('\xe5\xbe\x88\n', 6)('\xe6\xb2\xa1\n', 6)('\xe5\xae\x89\xe8\xa3\x85\n', 5)('\xe4\xbd\xbf\xe7\x94\xa8\n', 3)
convert.py
out = open('output.txt', 'w')f = open('test.txt', 'r')out.write(f.read().decode('string-escape'))
没有所谓的把乱码转换成中文一说。无论中文还是英文,在计算机里都会编码。
汉字的编码中:
当你想在控制台看见他们汉字字面情况,直接 print 即可。无所谓转换不转换。如果不打印,编码都是和这差不多,都是一些字符。
当然,这两种是可以转换的
pythonIn [1]: s = '\xe5\xae\x89\xe8\xa3\x85'In [2]: print s安装In [3]: u = s.decode('utf-8')In [4]: uOut[4]: u'\u5b89\u88c5'In [5]: print u安装In [6]: ss = u.encode('utf-8')In [7]: ssOut[7]: '\xe5\xae\x89\xe8\xa3\x85'In [8]: ss == sOut[8]: TrueIn [9]: type(s)Out[9]: strIn [10]: type(u)Out[10]: unicode
通常情况下,Python2 内部的字符串有 str 和 unicode。当你要把字符写入文件中,例如一个txt或者html文件,现在的文件都习惯用utf-8编码。所以你需要把str转换成utf-8输出,那么到时候打开txt或者html才能看见中文,否则那时出现的才是乱码。
你是直接打的tuple吧,像这样:
>>> a=("否否", 5)>>> print(a)('\xe5\x90\xa6\xe5\x90\xa6', 5)>>>
直接print字符串就行:
>>> a=("否否", 5)>>> print(a[0])否否
编橙之家文章,
相关内容
- Python默认参数调用疑惑,python参数疑惑,python文档中关于
- Django学习遇到瓶颈要如何继续学习,django瓶颈,RT,目前
- python class中的self _init_(self,param1,param2)怎么回事?,p
- Python switch case实现操作是怎样的,pythonswitch,Python里怎么
- Python模拟登录cookie有验证码要怎么做才能抓到内容,
- python html兼容空格问题,pythonhtml兼容空格,文本框里输入
- Python django监测用户跳转页面状态求思路,pythondjango,d
- 求python访问网页并提交请求的操作思路,python思路,我要
- 求助python django-pagination分页url显示方式问题,,django-p
- 新手求Python高手看源码错误,python源码,num=10;print
评论关闭