Iterate a list along with reading a file

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 runs two characters together in an “S” and adds a space. He then enters a new line for every 47 characters and copies the output to final.txt.

final.txt as follows:

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 would like to add 000to the first line, then 001to the second line and 003to the third line, etc.

So, I created a list with these values, and I'm trying to iterate through the strings along with the list items.

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 this is adding all the items to the list on the first line. The first element of the list should be added to the beginning of the first line, which means the second, third, etc.

+4
source share
2 answers

Read from 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

, , :

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() , . format() . {:03d} , . {}\n .

+3

, , :

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)

before:
['line1\n', 'line2\n', 'line3\n']

after:
['000 line1\n', '001 line2\n', '002 line3\n']
0

Source: https://habr.com/ru/post/1627519/


All Articles