TypeError:尝试从十六进制转换为字符串时找到的非十六进制数字

时间:2016-12-09 16:55:31

标签: python hex decode typeerror

我有一个用十六进制编码的文件,我正在尝试解码该文件但是我一直遇到类型错误。我只使用python开启和关闭几周,所以如果这似乎是一个基本的问题我道歉。

文件内容如下:

4647525137454353554e54544b5831375a42524d345742473246563639554e4a36495a3359304f35394843554637564d4d464f32354143574f495a4f4a4a565849373259544f46335a4358494b424e335047545a51534b47465259475956584d44594f473536494553373653455932574b33574431435a314d35545957594d4e57434444344948324d375858544f4c564f31444a45304947394c32375a584f4845535a534f43353859594c55594e4239363759393738313557475859345a474448434e4f5a5744544d696c6c656e69756d323030303a3035303233626566343737386639343461626439346334653364623062326166

这是我运行的代码:

"received_files/documents/cache/OCAGS0WFYO57JVFGUI4Z437.txt".decode("hex")

这就是我的回复:

Traceback (most recent call last):
  File "converter.py", line 1, in <module>
    "received_files/documents/cache/OCAGS0WFYO57JVFGUI4Z437.txt".decode("hex")
  File "/usr/lib/python2.7/encodings/hex_codec.py", line 42, in hex_decode
    output = binascii.a2b_hex(input)
TypeError: Non-hexadecimal digit found

1 个答案:

答案 0 :(得分:2)

你给它一个文件名而不是那个文件的内容:

"received_files/documents/cache/OCAGS0WFYO57JVFGUI4Z437.txt".decode("hex")

试试这个:

open("received_files/documents/cache/OCAGS0WFYO57JVFGUI4Z437.txt").read().decode("hex")