如何将包含十六进制数组的numpy文件转换为二进制文件?

时间:2017-02-15 13:02:41

标签: python numpy

在我的第一个问题How to convert an hexadecimale line in a text file to an array (Python)?之后,我需要将包含十六进制数组的numpy文件转换为二进制文件,My 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'],
 ['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']]

我尝试使用此代码:

import numpy as np
import struct
textin = np.load('/home/msmache/Bureau/OutFilePlaintexts.npy')
f = open('/home/msmache/Bureau/Plaintexts', 'wb')
for t in textin.flatten():
    f.write(struct.pack('h', t))
f.close()

我发现了这个错误:

f.write(struct.pack('h', t))
error: pack expected 3 items for packing (got 1)

0 个答案:

没有答案