I have a txt file with about 13000 lines, I'm importing them and when I want to make a loop to create a file with the information reordered as I need, I can't get it to work.
The file information is similar to this (the part with the line number is added for reference, it is not originally in the file):
Linea 0: A 213992,"A 114416","05/01/2021","19/01/2021","N","E","1005","* "," 0"," 0"," 0"," 0"," 0", \n
Linea 1: A 114416,"Nombre 1 ","* ","Address 1 ","CAPITAL ","4682551534 "," 74","F","1041","* "," 0"," "," ","17/01/2013","17/01/2013","1202","1505254500070100 ","DNI", \n
Linea 2: C,"37111662 ","01/05/1938","* ","2","* ", \n
Linea 3: G
Linea 4: A 213992," "," "," "," "," ",
Linea 5: 13
Linea 6: 1 475I 1
Linea 7: 2 941I 1
Linea 8: 3 190I 1
Linea 9: 4 192I 1
Linea 10: 5 412I 1
Linea 11: 6 4811I 1
Linea 12: 7 865I 1
Linea 13: 8 867I 1
Linea 14: 9 902I 1
Linea 15: 10 8298I 1
Linea 16: 11 546I 1
Linea 17: 12 711I 1
Linea 18: 13 120N 1
Linea 19: A 213993,"A 129320","05/01/2021","12/01/2021","N","E","1005","* "," 0"," 0"," 0"," 0"," 0",
Linea 20: A 129320,"Nombre 2 ","* ","Address 2 ","CAPITAL ","4601555620 11531906 "," 73","F"," 0","DNI 5564842 "," 0"," "," ","05/01/2021","05/01/2021","1005","1504600761690600 ","DNI",
Linea 21: C,"569456742 ","11/10/1947","* ","2","* ",
Linea 22: G
Linea 23: A 213993," "," "," "," "," ",
Linea 24: 12
Linea 25: 1 475I 1
Linea 26: 2 653I 1
Linea 27: 3 746I 1
Linea 28: 4 133I 1
Linea 29: 5 192I 1
Linea 30: 6 362I 1
Linea 31: 7 412I 1
Linea 32: 8 4811I 1
Linea 33: 9 546I 1
Linea 34: 10 902I 1
Linea 35: 11 948I 1
Linea 36: 12 120N 1
Linea 37: A 214012,"A 129321","04/01/2021","18/01/2021","N","E","1005","* "," 0"," 0"," 0"," 0"," 0",
Linea 38: A 129321,"Nombre 3 ","* ","Address 3 ","CAPITAL ","1123101950 "," 86","F"," 0","DNI 16561081 "," 0"," "," ","04/01/2021","04/01/2021","1005","1505370046050100 ","DNI",
Linea 39: C,"16561 ","02/03/1934","* ","2","* ",
Linea 40: G
Linea 41: A 214012," "," "," "," "," ",
Linea 42: 11
Linea 43: 1 475I 1
Linea 44: 2 192I 1
Linea 45: 3 297I 1
Linea 46: 4 412I 1
Linea 47: 5 4811I 1
Linea 48: 6 546I 1
Linea 49: 7 865I 1
Linea 50: 8 866I 1
Linea 51: 9 867I 1
Linea 52: 10 500I 1
Linea 53: 11 8298I 1
Linea 54: A 214013,"A 125271","04/01/2021","13/01/2021","N","E","1005"," 136 "," 0"," 0"," 0"," 0"," 0",
Linea 55: A 125271,"Nombre 4 ","* ","Address 4 ","CAPITAL ","4605-5813 "," 69","M"," 136","DNI 6151369 "," 0"," "," ","27/10/2017","27/10/2017","1005","1506000320150300 ","DNI",
Linea 56: C,"61546369 ","01/11/1948","* ","2","* ",
Linea 57: G
Linea 58: A 214013," "," "," "," "," ",
Linea 59: 2
Linea 60: 1 412I 1
Linea 61: 2 500I 1
From line 0 I need element 0 and 2, from line 1 field 1, from line 2 field 16 to that I concatenate 475 from l6. This has to be done the number of times that line 5 says .
There you have to start again with the process in line 19 (which would become 0) and thus go through the 13000 lines...
The result should be:
A213992,05/01/2021,070100 , ,Nombre 1 ","*,475
A213992,05/01/2021,070100 , ,Nombre 1 ","*,941
A213992,05/01/2021,070100 , ,Nombre 1 ","*,190
A213992,05/01/2021,070100 , ,Nombre 1 ","*,192
A213992,05/01/2021,070100 , ,Nombre 1 ","*,412
A213992,05/01/2021,070100 , ,Nombre 1 ","*,4811
A213992,05/01/2021,070100 , ,Nombre 1 ","*,865
A213992,05/01/2021,070100 , ,Nombre 1 ","*,867
A213992,05/01/2021,070100 , ,Nombre 1 ","*,902
A213992,05/01/2021,070100 , ,Nombre 1 ","*,8298
A213992,05/01/2021,070100 , ,Nombre 1 ","*,546
A213992,05/01/2021,070100 , ,Nombre 1 ","*,711
A213992,05/01/2021,070100 , ,Nombre 1 ","*,120
A213992,05/01/2021,070100 , ,Nombre 2 ","*,475
A213992,05/01/2021,070100 , ,Nombre 2 ","*,653
A213992,05/01/2021,070100 , ,Nombre 2 ","*,746
But Name 1 should be about 13 lines (q is variable, and is what defines the number of lines to print per Name), there it should jump to Name 2 recalculating the r0
(initial line) and putting the substrings of the first 3 lines concatenated with the q lines that follow.
Surely there is an easier way to do it than the loop I did and it doesn't work, and that is the following:
with open ("E:\SkyDrive\A Files\A Files\LACVI\Datos LABSYS\PROT2101 test - COPY.txt", "r") as filename:
lines = filename.readlines()
cantidad=len(lines)
r0 = 0
q = int(lines[r0+5])
t = r0 + 6
print (q , t)
while r0 < r0+q+t:
linea1 = lines[r0]
r0 = r0+1
linea2 = lines[r0]
r0 = r0+5
linea3 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea3[4:12].strip(), sep=",")
linea4 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea4[4:12].strip(), sep=",")
linea5 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea5[4:12].strip(), sep=",")
linea6 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea6[4:12].strip(), sep=",")
linea7 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea7[4:12].strip(), sep=",")
linea8 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea8[4:12].strip(), sep=",")
linea9 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea9[4:12].strip(), sep=",")
linea10 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea10[4:12].strip(), sep=",")
linea11 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea11[4:12].strip(), sep=",")
linea12 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea12[4:12].strip(), sep=",")
linea13 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea13[4:12].strip(), sep=",")
linea14 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea14[4:12].strip(), sep=",")
linea15 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea15[4:12].strip(), sep=",")
linea16 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea16[4:12].strip(), sep=",")
linea17 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea17[4:12].strip(), sep=",")
linea18 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea18[4:12].strip(), sep=",")
linea19 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea19[4:12].strip(), sep=",")
linea20 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea20[4:12].strip(), sep=",")
linea21 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea21[4:12].strip(), sep=",")
linea22 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea22[4:12].strip(), sep=",")
linea23 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea23[4:12].strip(), sep=",")
linea24 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea24[4:12].strip(), sep=",")
linea25 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea25[4:12].strip(), sep=",")
linea26 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea26[4:12].strip(), sep=",")
linea27 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea27[4:12].strip(), sep=",")
linea28 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea28[4:12].strip(), sep=",")
linea29 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea29[4:12].strip(), sep=",")
linea30 = lines[r0]
r0 = r0+1
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea30[4:12].strip(), sep=",")
linea31 = lines[r0]
print(linea1[1:13].replace(" ", ""),linea1[31:41],linea2[342:354],linea2[354:356],linea2[16:50].rstrip(),linea31[4:12].strip(), sep=",")
r0=q+7
print(r0)
break'''
I don't know if there is an easier way to create the lines to print (for now it's the one I found), I'm just starting out and I don't understand why the loop doesn't work.
I think I have understood what you are looking for, but the information you give in the question is wrong or incomplete, because using it does not give me exactly what you show should.
I've had to tweak some of the indices you use in the slices , as it looks like your input file contains a few more (or fewer) characters than you show in the example, perhaps some leading space. In any case, from the code that I give you, it should not be difficult to adapt it to your real case.
The idea is the following:
The main program uses a loop in which in each iteration it reads 6 lines from the file, and extracts from the last read line a value N, which it uses to read the next N lines as well.
To do this I make a loop
for linea in fichero
, but inside the loop I make itnext(fichero)
to read one more line each time I need it. That's why each iteration of the loop ends up reading 6+N lines.The first 6 lines are saved in one list, the next N in another.
These two lists are passed to a function, which is the one that does the "dirty work" of extracting from each of those lines the fields that interest you, and using them to compose with them N lines in the output
The use of the loop, the lists and the function greatly simplifies the code avoiding all the repetitions.
It looks like this:
And what I get (it is not exactly the same as what you put, because I am afraid as I said before that my input file is not exactly the same as yours) is the following: