2012-09-12 72 views
3

我需要讀取二進制文件中特定字符串的位置,然後對隨後的字節進行操作。該字符串是'colr'(這是一個JPEG 2000文件),這裏是我到目前爲止有:Python - 從二進制文件中讀取字符串

from collections import deque 

f = open('my.jp2', 'rb') 
bytes = deque([], 4) 
while ''.join(map(chr, bytes)) != 'colr': 
    bytes.appendleft(ord(f.read(1))) 

,如果這個工程:

bytes = deque([0x63, 0x6F, 0x6C, 0x72], 4) 
print ''.join(map(chr, bytes)) 

(返回 'COLR'),我不確定爲什麼我的循環中的測試永遠不會評估爲True。我旋轉 - 只是掛着 - 當我讀完整個文件時,我甚至沒有退出。

+0

你有看看http://stackoverflow.com/questions/6822725/rolling-or-sliding-window-iterator-in-python? –

+0

@ Jan-Philip - 謝謝!我應該看看適應其中之一。首先,雖然,這個答案http://stackoverflow.com/a/6822761/714478讓我意識到,我只是追加到deque的錯誤的一面,我的方法,以上的糾正,工作得很好! – JStroop

回答

2

更改bytes.appendleft()bytes.append(),然後它會工作 - 它爲我做。

0
with open("my.jpg","rb") as f: 
     print f.read().split("colr",1) 

如果你不想一次閱讀完這一切......然後

def preprocess(line): 
    print "Do Something with this line" 
def postprocess(line): 
    print "Do something else with this line" 
currentproc = preprocess 
with open("my.jpg","rb") as f: 
    for line in f: 
     if "colr" in line: 
      left,right = line.split("colr") 
      preprocess(left) 
      postprocess(right) 
      currentproc= postprocess 
     else: 
      currentproc(line) 

由線而不是逐字節的線......但我沒意見... 我很難時間以爲你沒有足夠的內存來保存在內存中的整個JPG ...蟒是不是一個真正的真棒語言來減少內存或時間的腳印 但它是真棒對功能要求:)

+0

這可以工作,但我應該已經更清楚了,我需要繼續閱讀文件,提取其他信息。您是否願意詳細說明,如果可能,展示如何繼續閱讀文件對象? – JStroop

+0

你運行過嗎? –

+0

該文件大於37 MB,並且該字符串應該小於60個字節左右,所以我再次想知道如何在split()後繼續讀取字節'f' – JStroop

相關問題