Reputation: 41
import os
import sys
import textwrap
string123="00505661e418005056618f67080045000086000040004011c1bd1e1e1e321e1e1e3cc0e62118007200000800000000000100000c298a92ba000c29f914ea080045000054c757400040015a93464646144646461e080031e3470d000142bdaf5600000000cb27030000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637"
j=0
i = 0
string1234 = ''
while i < 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=""
for lines in g:
while j < len(my_list):
line_new = my_list[j]+" "+ lines
print line_new
lines+=1
j+=1
This script actually spaces the every two characters together in "S" and adds a space. Then it introduces a new line for every 47 characters and copies the output to final.txt
.
final.txt
looks like 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 to prepend 000
to first line, then 001
to second line and 003
to third line and so on.
So I created a list with those values and I am trying to iterate both the lines along with the list elements.
my_list=["000","001","002","003","004","005","007","008","009"]
line_new=""
for lines in g:
while j < len(my_list):
line_new = my_list[j]+" "+ lines
print line_new
lines+=1
j+=1
But the it is appending all the elements in the list to the first line. The first element of list should be prepended to the beginning of first line and so with second, third, etc.
Upvotes: 4
Views: 82
Reputation: 85462
final.txt
from __future__ import print_function
with open("final.txt") as f:
for 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
final.txt
in the first placeAssuming that you want to add the numbers to the file, you can do this:
final = string1234.strip()
with open("final.txt","w") as f:
for 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 you the index for your iteration, starting from zero. The format()
method formats the output line. {:03d}
inserts a integer with leading zeros taking up three places. The following {}\n
takes the text of the line and adds a newline character at the end.
Upvotes: 3
Reputation: 123609
If you are looking for a way to prepend the line numbers onto existing lines that you've read from the file then this is one way to do it:
f = open("final.txt","r")
g = f.readlines()
print("before:")
print(g)
print("")
g1 = [format(i, "03d") + " " + g[i] for i in range(len(g))]
print("after:")
print(g1)
which produces
before:
['line1\n', 'line2\n', 'line3\n']
after:
['000 line1\n', '001 line2\n', '002 line3\n']
Upvotes: 0