2017-07-27 72 views
0

我有一個.raw文件,其中包含一個52行html標頭,後面跟着數據本身。該文件編碼在little-endian 24bits SIGNED,我想將數據轉換爲ASCII文件中的整數。我使用Python 3將Little-endian 24位文件轉換爲ASCII數組

我想 '解壓' 與this post發現下面的代碼將整個文件:

import sys 
import chunk 
import struct 

f1 = open('/Users/anais/Documents/CR_lab/Lab_files/labtest.raw', mode = 'rb') 
data = struct.unpack('<i', chunk + ('\0' if chunk[2] < 128 else '\xff')) 

但我收到此錯誤信息:

TypeError: 'module' object is not subscriptable 

編輯

它似乎這是更好的:

data = struct.unpack('<i','\0'+ bytes)[0] >> 8 

但我仍然得到一個錯誤信息:

TypeError: must be str, not type 

容易解決我相信?

+0

你能發表'f1.read()'的結果嗎? – Tomalak

+1

1)屏幕轉儲不受歡迎:大容量存儲空間,不可重複使用,不可搜索2)問題在於* chunk *模塊。可能是模塊名稱和您選擇的實例變量之間的名稱衝突。或者你忘了實例化* Chunk *類的東西呢? – guidot

+0

您需要首先從HTML中分離二進制數據。不要使用'bytes'作爲變量名稱,因爲它與Python自己的'bytes'類型衝突 –

回答

0

這不是一個很好的文件在Python中處理! Python非常適合處理文本文件,因爲它在內部緩衝區中以大塊的形式讀取它們,然後在線上進行迭代,但不能輕鬆訪問文本讀取之後出現的二進制數據。此外,struct模塊不支持24位值。

我能想象的唯一方法是一次讀取一個字節的文件,首先跳過52行結束行,然後每次讀取字節3,將它們連接成4字節的字節串並解壓縮。

可能的代碼可以是:

eol = b'\n'   # or whatever is the end of line in your file 
nlines = 52   # number of lines to skip 

with open('/Users/anais/Documents/CR_lab/Lab_files/labtest.raw', mode = 'rb') as f1: 

    for i in range(nlines):  # process nlines lines 
     t = b''     # to store the content of each line 
     while True: 
      x = f1.read(1)  # one byte at a time 
      if x == eol:   # ok we have one full line 
       break 
      else: 
       t += x   # else concatenate into current line 
     print(t)     # to control the initial 52 lines 

    while True: 
     t = bytes((0,))    # struct only knows how to process 4 bytes int 
     for i in range(3):   # so build one starting with a null byte 
      t += f1.read(1) 
     # print(t) 
     if(len(t) == 1): break  # reached end of file 
     if(len(t) < 4):    # reached end of file with uncomplete value 
      print("Remaining bytes at end of file", t) 
      break 
     # the trick is that the integer division by 256 skips the initial 0 byte and keeps the sign 
     i = struct.unpack('<i', t)[0]//256 # // for Python 3, only/for Python 2 
     print(i, hex(i))      # or any other more useful processing 

注:上面的代碼假定您的52行描述(由線的端部封端的)是真實的,但示出的圖像讓認爲最後一行是沒有的。在這種情況下,您應該先計算51行,然後跳過最後一行的內容。

def skipline(fd, nlines, eol): 
    for i in range(nlines):  # process nlines lines 
     t = b''     # to store the content of each line 
     while True: 
      x = fd.read(1)  # one byte at a time 
      if x == eol:   # ok we have one full line 
       break 
      else: 
       t += x   # else concatenate into current line 
     # print(t)     # to control the initial 52 lines 

with open('/Users/anais/Documents/CR_lab/Lab_files/labtest.raw', mode = 'rb') as f1: 
    skiplines(f1, 51, b'\n')  # skip 51 lines terminated with a \n 
    skiplines(f1, 1, b'>')  # skip last line assuming it ends at the > 

    ... 
+0

非常感謝您的回答,附有詳細的解釋,這對我來說是必要的,因爲我剛開始編程。 我用Matlab代碼對結果進行了交叉檢查,沒有意外,它完美的工作!再次感謝 !! – ananas