I am trying to copy data from error.pat and edit it in a new format, but now and then I encounter below errors:
IndexError: string index out of range
Below is the format I want( list 20 hex number on each line)
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)
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:
%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. 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.
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.