簡體   English   中英

Python3字符串到文件中的十六進制錯誤但在終端中沒有

[英]Python3 string to hex error in file but in terminal not

這是我在輸入線索[0] = '706965' 處輸出的代碼:

x = clues[0]
answer = bytes.fromhex(x).decode("ascii")
<class 'ValueError'>, ValueError('non-hexadecimal number found in fromhex() arg at position 5')

如果我將 0 添加到線索 [0] 中,它會輸出預期的 output ,其中第一個字符帶有偏移量。

x = '0'+clues[0]

output 則為“[]ie”。 [] 表示特殊字符(stackoverflow 忽略它)。

但是如果我在終端運行這個例子,一切正常。

>>> x = '706965'
>>> bytes.fromhex(x).decode("ascii")
'pie'

已解決:解析錯誤

來自bytes.fromhex文檔

這個 bytes class 方法返回一個 bytes object,解碼給定的字符串 object。 字符串每個字節必須包含兩個十六進制數字,忽略 ASCII 空格。

在您的字符串前面加上 '0' 使其長度為七,因此它不能由兩位數對組成

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM