2016-07-06 3073 views
1

我做的這一個荏苒FUNC:struct.pack在Python3了struct.error:需要的參數不是整數

open_file = open(file_path,'rb') 
open_save = open(save_path,'wb+') 
try: 
    open_read = open_file.read() 
    data = zip_data(open_read) 

    head_xis = b'XIS' 
    head_version = bytes(0) 
    head_type1 = bytes(1) 
    head_type2 = bytes(1) 
    head_en_type = bytes(0) 
    head_com_type = bytes(1) 
    head_sour_len = len(open_read) 

    # try: 
    md5 = hashlib.md5() 
    md5.update(open_read) 
    head_md5 = md5.digest() 
    print("byteeeeee") 
    # except: 
    #  head_md5 = None 
    randoms = str(uuid.uuid1()).split('-')[0] 
    head_random = bytes(randoms,encoding = "utf-8") 
    print(head_md5) 
    print(head_random) 
    head_resour_len= len(data) 

    print(type(head_xis)) 
    print(type(head_version)) 
    print(type(head_type1)) 
    print(type(head_type2)) 
    print(type(head_en_type)) 
    print(type(head_com_type)) 
    print(type(head_sour_len)) 
    print(type(head_md5)) 
    print(type(head_random)) 
    print(type(head_resour_len)) 

    head = struct.pack('3sBBBBBI16s8sI', 
         head_xis, 
         head_version, 
         head_type1, 
         head_type2, 
         head_en_type, 
         head_com_type, 
         head_sour_len, 
         head_md5, 
         head_random, 
         head_resour_len 
         ) 
    open_save.write(head) 

# except Exception as e: 
#  print("eeeee" + str(e)) 
#  return False,str(e) 
# else: 
#  open_save.write(data) 
#  return True,'' 
finally: 
    open_file.close() 
    open_save.close() 

,它顯示了異常,並打印象下面這樣:

byteeeeee 
b'\xf9\xf4\xf2\xcb\xbfM\x11\xb5\xeeNP/\x02H\xebK' 
b'f9f33502' 
class 'bytes' 
class 'bytes' 
class 'bytes' 
class 'bytes' 
class 'bytes' 
class 'bytes' 
class 'int' 
class 'bytes' 
class 'bytes' 
class 'int' 
Traceback (most recent call last): 
    File "entrance.py", line 52, in startProcess 
    Helper.processCombine(self.selectedVer,False) 
    File "/Users/mapzchen/myShell/qtPro/fastColuaco/Helper.py", line 86, in processCombine 
    itemConf.get(version,suffix),True,True) 
    File "/Users/mapzchen/myShell/qtPro/fastColuaco/coluaco.py", line 514, in process 
    compress(build_dir,zip_dir,filter_file) 
    File "/Users/mapzchen/myShell/qtPro/fastColuaco/coluaco.py", line 400, in compress 
    zfile = zip_file(src_file_path,save_path) 
    File "/Users/mapzchen/myShell/qtPro/fastColuaco/coluaco.py", line 131, in zip_file 
    head_resour_len 
struct.error: required argument is not an integer 

我試圖打印類型的參數, 它似乎正確地安裝3sBBBBBI16s8sI 我被什麼ARG執行此異常困惑

回答

0

In structure formating string "B" for bytes need variable type 'int', that is main problem. So instead of head_type1 = bytes(1) use head_type1 = 1

下面給出的代碼給出了相同的錯誤。

問題:

​​

解決方案:

n = 2 
    x = struct.pack("B", n) 

建議: 使用的字節順序,如 '=',在處理像 '我' 的整數。