`
rokuan
  • 浏览: 19711 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

python转码

 
阅读更多
首先用utf8编码形式写一个测试用的txt文件
>>> file = open('test.txt','r')
>>> txt = file.read()
>>> txt
'\xe8\xbf\x99\xe6\x98\xaf\xe4\xb8\x80\xe4\xb8\xaa\xe6\xb5\x8b\xe8\xaf\x95\xe6\x96\x87\xe4\xbb\xb6\nthis is a test file \n'
>>> print txt
这是一个测试文件
this is a test file 
>>> u_txt = txt.decode('utf-8')
>>> u_txt
u'\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5\u6587\u4ef6\nthis is a test file \n'
>>> print u_txt
这
>>>file.close()

在解释器输入txt,显示出读入的字节内容
然后使用print打印出来,我猜想应该是print函数自动将内容解码了,就像接下来的步骤。u_txt的内容就是解码后的内容,我们看到python把它转换成了python通用的unicode编码,所以print应该是把特定的编码解码后都转成了unicode编码。接着我们就又可以对其进行编码了。
>>> file = open('test_gbk.txt','w')
>>> u_txt
u'\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5\u6587\u4ef6\nthis is a test file \n'
>>> file.write(u_txt.encode('gbk'))
>>> file.close()
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics