我试图解码一个包含十六进制的txt文件。我一直得到一个ValueError:在fromhex()arg中的位置2处发现了非十六进制数字错误

2024-05-17 09:02:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我到处寻找解决办法,但没有一个奏效。 下面是我一直在处理Python3的代码

IPFS = open("Q.txt",)

Q = IPFS.read()

print(Q)

IPFS.close()

encoded = bytearray.fromhex(Q).decode()

print(encoded)

在Q.txt文件中有这个字符串

51

如果你知道是什么把我搞砸了,如果你告诉我,我会非常感激的!:D


Tags: 文件代码txtclosereadopenpython3print
2条回答

我可以使用Python 3.9,我得到以下输出:

51 
Q

你确定你的档案里没有其他东西,或者我不知道。。。尝试Ctrl+a并删除全部,然后重写51

对我来说,在Python 3.8.5上也可以很好地工作。我猜在1号之后你会有一条新线。根据以下文件,3.7版中的行为发生了变化:

classmethod fromhex(string) This bytearray class method returns bytearray object, decoding the given string object. The string must contain two hexadecimal digits per byte, with ASCII whitespace being ignored.

Changed in version 3.7: bytearray.fromhex() now skips all ASCII whitespace in the string, not just spaces.

相关问题 更多 >