2017-02-15 119 views
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' 

回答

3

Plaintext=['{:02x}'.format(b) for b in line]不行,因爲for b in line發出行的字符,而你需要他們的2對每個迭代。

此外,即使您這樣做了,十六進制格式適用於整數值,而不是字符串。

我建議創建all_arrays:使用簡單的列表切片雙列表理解:

al_arrays = [[l[i:i+2] for i in range(0,len(l.strip()),2)] for l in In_f] 

all_arrays收益率預期:

[['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'], 
['82', 'e5', 'a7', 'a0', '21', '19', '7f', '6f', 'be', '94', 'a8', '67', 'd4', 
    'bb', '38', '95'], 
['85', '05', '80', 'c1', 'ff', 'ec', '88', '7c', '50', '00', 'c9', 'b3', 'a0', 
    'e6', 'b3', '9d'], 
['15', '26', 'af', '37', 'ce', '4b', '0b', '4e', '81', 'f8', 'af', '06', '47', 
    'e3', '71', '19'], 
['ba', 'b1', '9c', '53', 'fd', '86', 'e6', 'af', 'c9', '33', '27', '6b', '28', 
    '6e', '0c', '36'], 
['b5', '2e', '53', '00', '7b', 'eb', 'e8', '87', '7c', 'e5', '69', 'ad', '24', 
    '94', 'dd', '76'], 
['44', 'fd', '87', 'e9', 'b1', 'a4', '0a', '92', '9a', 'b6', '13', '56', '65', 
    'c2', '2d', '0f'], 
['a8', '8e', '51', '41', 'dd', 'c9', '9e', '02', '07', 'a9', 'e1', '44', 'f4', 
    '01', '0a', '22'], 
['58', 'ff', '59', '78', '19', 'ea', '0a', 'a3', '70', '24', 'a1', 'f1', 'f8', 
    '4c', '52', '24']] 

注意:如果你想要的數值,而不是十六進制字符串,只是轉換爲整數(以16爲底)

al_arrays = [[int(l[i:i+2],16) for i in range(0,len(l.strip()),2)] for l in In_f] 

你會得到這樣的:

[[122, 142, 93, 195, 144, 120, 30, 171, 141, 242, 192, 144, 191, 75, 235, 202], [219, 172, 15, 186, 85, ... 

不是十六進制,但不要緊,二進制轉換。

+0

非常感謝您的幫助,它的工作原理! – nass9801

+0

如何避免字符串顯示。我的意思是我需要這種格式:[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

+1

你的意思是你想要十六進制數字?很容易,但是你會在列表中有'0x'前綴。我認爲這就是你想要的:數字。讓我編輯我的答案。編輯。 –

相關問題