3
我在每一行包含一個十六進制的明文的文本文件,我的文件看起來像這樣:如何將文本文件中的十六進制行轉換爲數組(Python)?
7a8e5dc390781eab8df2c090bf4bebca
dbac0fba55d3d4fc177161bfe24dc7fb
82e5a7a021197f6fbe94a867d4bb3895
850580c1ffec887c5000c9b3a0e6b39d
1526af37ce4b0b4e81f8af0647e37119
bab19c53fd86e6afc933276b286e0c36
b52e53007bebe8877ce569ad2494dd76
44fd87e9b1a40a929ab6135665c22d0f
a88e5141ddc99e0207a9e144f4010a22
58ff597819ea0aa37024a1f1f84c5224
我需要每一行轉換爲數組,我的意思是說我的numpy的文件必須是這樣的:
[
[7a,8e,5d,c3,90,78,1e,ab,8d,f2,c0,90,bf,4b,eb,ca],[db,ac,0f,ba,55,d3,d4,fc,17,71,61,bf,e2,4d,c7,fb],
......., ]
import numpy as np
In_path= "/home/msmache/Bureau/testPlaintext.txt"
Out_path= "/home/msmache/Bureau/testPlaintext.npy"
with open(In_path, "r") as In_f:
all_arrays = []
Plaintext=[]
for line in In_f:
Plaintext=['{:02x}'.format(b) for b in line]
all_arrays.append(Plaintext)
print all_arrays
with open(Out_path, "wb") as Out_f:
np.save(Out_path, all_arrays)
data = np.load(Out_path)
print data
這是我的錯誤:
Plaintext=['{:02x}'.format(b) for b in line]
ValueError: Unknown format code 'x' for object of type 'str'
非常感謝您的幫助,它的工作原理! – nass9801
如何避免字符串顯示。我的意思是我需要這種格式:[7a,8e,5d,c3,90,78,1e,ab,8d,f2,c0,90,bf,4b,eb,ca],不是這個:['7a' ,8e,5d,c3,90,78,1e,ab,8d,f2,c0,90,bf, '4b','eb','ca'],因爲我發現一個問題將numpy文件轉換爲二進制文件。 – nass9801
你的意思是你想要十六進制數字?很容易,但是你會在列表中有'0x'前綴。我認爲這就是你想要的:數字。讓我編輯我的答案。編輯。 –