Python在字符后读取一定数量的字节

时间:2015-01-16 19:19:02

标签: python parsing hex offset

我正在处理一个字符分隔的hex文件,其中每个字段都有一个特定的起始代码。我已经将文件打开为“rb”,但我想知道,在使用.find得到起始码的索引后,如何从该位置读取一定数量的字节? 这就是我加载文件的方式以及我尝试做的事情

with open(someFile, 'rb') as fileData:
    startIndex = fileData.find('(G')
    data = fileData[startIndex:7]

其中7是我想从find函数返回的索引中读取的字节数。我使用的是python 2.7.3

1 个答案:

答案 0 :(得分:1)

您可以在python2.7下的字节字符串中获取子字符串的位置,如下所示:

>>> with open('student.txt', 'rb') as f:
...     data = f.read()
... 
>>> data  # holds the French word for student: élève
'\xc3\xa9l\xc3\xa8ve\n'
>>> len(data)  # this shows we are dealing with bytes here, because "élève\n" would be 6 characters long, had it been properly decoded!
8
>>> len(data.decode('utf-8'))
6
>>> data.find('\xa8')  # continue with the bytestring...
4
>>> bytes_to_read = 3
>>> data[4:4+bytes_to_read]  
'\xa8ve'

您可以查找特殊字符,并且为了与Python3k兼容,如果您在字符前添加b,表明这些字节是字节(在Python2.x中,它可以在没有虽然):

 >>> data.find(b'è')  # in python2.x this works too (unfortunately, because it has lead to a lot of confusion): data.find('è')
3
>>> bytes_to_read = 3
>>> pos = data.find(b'è')
>>> data[pos:pos+bytes_to_read] # when you use the syntax 'n:m', it will read bytes in a bytestring
'\xc3\xa8v'
>>>