我尝试将文本文件转换为numpy文件,我的文本文件在每行中包含一个密文:
a3140be40735f9f1865aa6b1b32b5667
23167dd91052ae65c5f27621d6e856a2
4d741dd44c079b12ff657e56eb9b2e0b
fc6a86ee980ee0fe36e48a65a6200053
86ce0634c82e6a3e049d76f62b4ff874
a3fbf406a651f41f015aee7e3452c4a3
7b7b1b6b8f0df7800c0495a1d72a3b14
2563cbbcce33a2097bdd017f89cff905
bd0f6cc7aae9ef7301183a6d3f855688
64d0470ec6bb72a3360cfa287c304eb5
598950560b06d10da657e0aab9a36c0e
df80c520099f1cb2605bf6780e000073
a0ae5abcd586de81093be28bc7929ef9
dcf021f46d6e12594aefb82185351b60
b6e332198239c0cc4d05f0d5a304a0b0
ecfae7851dce471b8cdb0f2c91822f9e
9a9e3f9f7b98f375f2b6a2eb008d1b17
173f5c6958a265e0bb00829e9facc0c5
我的文件包含203860行。我需要将每一行放在一个表中,而不是将所有表放在一个大表中:这是我的代码:
import numpy as np
In_path= r'Y:\Third_Traces\TextFiles\Ciphertexts_In.txt'
Out_path= r'Y:\Third_Traces\NumpyFiles\Ciphertexts_Out.npy'
all_arrays = []
with open(In_path, "r") as In_f:
#all_arrays = [[l[i:i+2] for i in range(0,len(l.strip()),2)] for l in In_f]
#all_arrays=map(int,arrays)
all_arrays = [[int(l[i:i+2], 16) for i in range(0, len(l.strip()), 2)] for l in In_f]
print (all_arrays)
with open(Out_path, "wb") as Out_f:
np.save(Out_path, all_arrays)
data = np.load(Out_path)
print (data)
我希望找到一个形状为(203860L,16L)的文件,但我发现它是(1L,3363690L),我不知道是什么问题?
答案 0 :(得分:0)
在你的代码all_arrays
中只是一个简单的python列表列表。改变这一行
all_arrays = [[int(l[i:i+2], 16) for i in range(0, len(l.strip()), 2)] for l in In_f]
到
all_arrays = np.array([[int(l[i:i+2], 16) for i in range(0, len(l.strip()), 2)] for l in In_f])
因此在将其保存到文件之前它是一个numpy数组。然后应该保留形状。