IndexError:字符串索引超出读取十六进制文件的范围

时间:2014-07-13 10:48:35

标签: python hex

我正在尝试从error.pat复制数据并以新格式对其进行编辑,但现在我遇到以下错误:

IndexError: string index out of range

Original data in error.pat  ( just part of this file)

以下是我想要的格式(每行列出20个十六进制数字)

45 72 4c 67 01 00 e5 00 00 00 00 00 04 03 00 03 a3 05 00 00
45 72 4c 67 02 00 07 00 01 00 00 00 d1 00 01 01 f4 05 4a 00
45 72 4c 67 03 00 07 00 01 00 00 00 0d 00 01 01 f4 05 4a 00
52 64 45 72 02 00 b4 22 da 21 97 22 88 22 fe 21 13 22 ec 21
45 72 4c 67 04 00 07 00 01 00 00 00 0d 00 01 01 f5 05 4a 00
52 64 45 72 00 00 b6 22 d8 21 98 22 8a 22 fd 21 fe 21 f1 21
45 72 4c 67 05 00 07 00 01 00 00 00 d1 00 01 01 f6 05 00 00
45 72 4c 67 06 00 07 00 01 00 00 00 0d 00 01 01 f6 05 00 00

下面是我的代码:

f = open('error.pat')

for line in f:
    for i in range(0,4):
        for j in range(0,4):
            for k in range(0,5):  
                print format(ord(line[i*20+j*5+k]),'02x'),
        print

当i的范围小于9时,我可以得到正常结果。下面是范围为(0,4)时的结果

45 72 4c 67 01 00 e5 00 00 00 00 00 04 03 00 03 a3 05 00 00
45 72 4c 67 02 00 07 00 01 00 00 00 d1 00 01 01 f4 05 4a 00
45 72 4c 67 03 00 07 00 01 00 00 00 0d 00 01 01 f4 05 4a 00
52 64 45 72 02 00 b4 22 da 21 97 22 88 22 fe 21 13 22 ec 21

但是当我把它作为(0,10)时,它会显示如下错误:

%run "C:/my_python_modules/original new trial.py"
45 72 4c 67 01 00 e5 00 00 00 00 00 04 03 00 03 a3 05 00 00
45 72 4c 67 02 00 07 00 01 00 00 00 d1 00 01 01 f4 05 4a 00
45 72 4c 67 03 00 07 00 01 00 00 00 0d 00 01 01 f4 05 4a 00
52 64 45 72 02 00 b4 22 da 21 97 22 88 22 fe 21 13 22 ec 21
45 72 4c 67 04 00 07 00 01 00 00 00 0d 00 01 01 f5 05 4a 00
52 64 45 72 00 00 b6 22 d8 21 98 22 8a 22 fd 21 fe 21 f1 21
45 72 4c 67 05 00 07 00 01 00 00 00 d1 00 01 01 f6 05 00 00
45 72 4c 67 06 00 07 00 01 00 00 00 0d 00 01 01 f6 05 00 00
52 64 45 72 01 00 48 22 4f 22 5e 22 72 22 fa 21
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
C:\Program Files\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
    195             else:
    196                 filename = fname
--> 197             exec compile(scripttext, filename, 'exec') in glob, loc
    198     else:
    199         def execfile(fname, *where):

C:\my_python_modules\original new trial.py in <module>()
      5         for j in range(0,4):
      6             for k in range(0,5):
----> 7                 print format(ord(line[i*20+j*5+k]),'02x'),
      8         print
      9 

IndexError: string index out of range 

我检查了原始文件,下面应该还有很多数据。

请帮助指出哪个部分和错误以及如何解决这个问题,谢谢。

1 个答案:

答案 0 :(得分:0)

您无法从包含二进制数据的文件中读取。如果要在每行显示 n 字节,请在每次循环迭代时读取 n 字节,然后转换并打印它们。您还应该以二进制模式打开二进制文件。否则,无法保证您获得文件1:1的内容,甚至不能获得所有内容。

from functools import partial


def main():
    bytes_per_line = 20
    with open('error.pat', 'rb') as binary_file:
        for block in iter(partial(binary_file.read, bytes_per_line), ''):
            print ' '.join('{0:02x}'.format(ord(b)) for b in block)


if __name__ == '__main__':
    main()