python - Iterating a list along with file read -
import os import sys import textwrap string123="00505661e418005056618f67080045000086000040004011c1bd1e1e1e321e1e1e3cc0e62118007200000800000000000100000c298a92ba000c29f914ea080045000054c757400040015a93464646144646461e080031e3470d000142bdaf5600000000cb27030000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637" j=0 = 0 string1234 = '' while < len(string123): #print string123[i:i+2] string1234 = string1234+' '+string123[i:i+2] i+=2 final = string1234.strip() final1= '\n'.join(textwrap.wrap(final, 47)) print final f=open("final.txt","w") f.write(final1) f.close f=open("final.txt","r") g=f.readlines() my_list=["000","001","002","003","004","005","007","008","009"] line_new="" lines in g: while j < len(my_list): line_new = my_list[j]+" "+ lines print line_new lines+=1 j+=1
this script spaces every 2 characters in "s" , adds space. introduces new line every 47 characters , copies output final.txt
.
final.txt
looks this:
00 50 56 61 e4 18 00 50 56 61 8f 67 08 00 45 00 00 86 00 00 40 00 40 11 c1 bd 1e 1e 1e 32 1e 1e 1e 3c c0 e6 21 18 00 72 00 00 08 00 00 00 00 00 01 00 00 0c 29 8a 92 ba 00 0c 29 f9 14 ea 08 00 45 00 00 54 c7 57 40 00 40 01 5a 93 46 46 46 14 46 46 46 1e 08 00 31 e3 47 0d 00 01 42 bd af 56 00 00 00 00 cb 27 03 00 00 00 00 00 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37
i wanted prepend 000
first line, 001
second line , 003
third line , on.
so created list values , trying iterate both lines along list elements.
my_list=["000","001","002","003","004","005","007","008","009"] line_new="" lines in g: while j < len(my_list): line_new = my_list[j]+" "+ lines print line_new lines+=1 j+=1
but appending elements in list first line. first element of list should prepended beginning of first line , second, third, etc.
read final.txt
from __future__ import print_function open("final.txt") f: index, line in enumerate(f): print('{:03d} {}'.format(index, line), end='')
output:
000 00 50 56 61 e4 18 00 50 56 61 8f 67 08 00 45 00 001 00 86 00 00 40 00 40 11 c1 bd 1e 1e 1e 32 1e 1e 002 1e 3c c0 e6 21 18 00 72 00 00 08 00 00 00 00 00 003 01 00 00 0c 29 8a 92 ba 00 0c 29 f9 14 ea 08 00 004 45 00 00 54 c7 57 40 00 40 01 5a 93 46 46 46 14 005 46 46 46 1e 08 00 31 e3 47 0d 00 01 42 bd af 56 006 00 00 00 00 cb 27 03 00 00 00 00 00 10 11 12 13 007 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 008 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 009 34 35 36 37
write final.txt
in first place
assuming want add numbers file, can this:
final = string1234.strip() open("final.txt","w") f: index, line in enumerate(textwrap.wrap(final, 47)): f.write('{:03d} {}\n'.format(index, line))
final.txt
:
000 00 50 56 61 e4 18 00 50 56 61 8f 67 08 00 45 00 001 00 86 00 00 40 00 40 11 c1 bd 1e 1e 1e 32 1e 1e 002 1e 3c c0 e6 21 18 00 72 00 00 08 00 00 00 00 00 003 01 00 00 0c 29 8a 92 ba 00 0c 29 f9 14 ea 08 00 004 45 00 00 54 c7 57 40 00 40 01 5a 93 46 46 46 14 005 46 46 46 1e 08 00 31 e3 47 0d 00 01 42 bd af 56 006 00 00 00 00 cb 27 03 00 00 00 00 00 10 11 12 13 007 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 008 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 009 34 35 36 37
enumerate()
gives index iteration, starting zero. format()
method formats output line. {:03d}
inserts integer leading zeros taking 3 places. following {}\n
takes text of line , adds newline character @ end.
Comments
Post a Comment