如何将文件中的某一段utf8的编码转义成中文内容?

现有1.txt文档,里面的内容是\xe6\x88\x90\xe9\x83\xbd

请问如何通过python读取这个文件的内容,然后显示成中文的形式?

阅读 5.7k
3 个回答

为了让问题有趣一点,让我来一行代码搞定它 =w=

python2 -c 'import binascii as b; from sys import stdin, stdout; stdout.write(b.unhexlify(stdin.read().strip().replace("\\x", "")))'
python3 -c 'import binascii as b; from sys import stdin, stdout; stdout.buffer.write(b.unhexlify(stdin.buffer.read().strip().replace(b"\\x", b"")))'
ascii2uni -qa7

最后一行是赠品 =w=

在用Python2?换Python3吧。

Python2下:

print('\xe6\x88\x90\xe9\x83\xbd') ==> '成都'
import os
f = open(r'2.txt').read().strip().replace("\\x","")
print f.decode("hex")
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题