python不能struct.pack struct.error:无法将参数转换为整数

时间:2015-07-06 05:31:28

标签: python

我从一个大小为1444352kB的hex文件中读取。我获取128个字节的数据并尝试使用python struct.pack打包它们。

以下是代码:

#!/usr/bin/env python
import os
import struct
import ctypes
import array
import binascii

import sys,getopt

filename = file_location
blocksize = 1444352

opts,args = getopt.getopt(sys.argv[1:],'f:b:')
for o,a in opts:
    if o == '-f':
    filename = a
if o == '-b':
    blocksize = a

offset = 0
with open(filename,"rb") as f:
block = f.read(blocksize)
str = ""
for ch in block:
    str += hex(ord(ch))+" "


sector = []
c = 0
for s in str.split(' ') :
    sector.append(s)
    c += 1
    if c == 128 :
        sector.append("")
        c = 0
        #print sector
        sector  = ', '.join(sector)
        #print sector
        print type(sector)
        sector = sector.split(',')
        secdata = []
        for items in sector[0:127] :
            secdata.append(items)
        secdata2 = ','.join(secdata)
        print secdata2
        print type(secdata2)
        struct.pack('B', secdata2)
        break

看似是列表的secdata,我已经转换为字符串。但是当我尝试打包128字节的数据时,我总是得到错误struct.error: cannot convert argument to integer

0 个答案:

没有答案