简体   繁体   English

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

[英]IndexError: string index out of range reading Hexadecimal file

I am trying to copy data from error.pat and edit it in a new format, but now and then I encounter below errors: 我正在尝试从error.pat复制数据并以新格式对其进行编辑,但是有时我会遇到以下错误:

IndexError: string index out of range

error.pat中的原始数据(仅是此文件的一部分)

Below is the format I want( list 20 hex number on each line) 下面是我想要的格式(每行列出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

below is my code: 下面是我的代码:

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 could get normal result when range of i is small than 9. Below is the result when range is (0,4) 当i的范围小于9时,我可以获得正常结果。下面是range为(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

But when I put it as (0,10), it would show error as below: 但是当我将其设置为(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 

I checked the original file, there should still be much data below. 我检查了原始文件,下面仍然应该有很多数据。

Pleaseh help instruct which part and wrong and how to solve this issue, thanks. 谢谢您的帮助,指示哪一部分是错误的以及如何解决此问题。

You can't read lines from a file with binary data. 您无法从具有二进制数据的文件中读取 If you want to display n bytes per line, read n bytes per loop iteration and then convert and print them. 如果要每行显示n个字节,请在每次循环迭代中读取n个字节,然后进行转换和打印。 Also you should open binary files in binary mode. 另外,您应该以二进制模式打开二进制文件。 Otherwise it is not guaranteed that you get the content of the file 1:1 and not even all of it. 否则,不能保证您获得文件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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM