Tengo una archivo txt con unas 13000 líneas, las estoy importando y cuando quiero hacer un bucle para armar un archivo con la información reordenada como necesito, no logro que funcione.
La información del archivo es similar a esta (la parte con el número de línea es un añadido para referencia, no está originalmente en el fichero):
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
De la linea 0 necesito el elemento 0 y 2, de la linea 1 el campo 1, de la linea 2 el campo 16 a eso le concatene el 475 de la l6.esto lo tiene que hacer la cantidad de veces que dice la linea 5.
Ahi tiene que empezar de nuevo con el proceso en la linea 19 (que pasaria a ser la 0) y asi recorrer las 13000 lineas...
El resultado tendria que ser:
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
Pero Nombre 1 deberían ser unas 13 líneas (q es es variable, y es el que define la cantidad de líneas a imprimir por Nombre), ahí debería saltar a Nombre 2 recalculando el r0
(la linea inicial) y poniendo las las subcadenas de las primeras 3 líneas concatenadas con las q líneas que siguen.
Seguramente hay un forma mas facil de hacerlo que el bucle que hice yo y no funciona, y que es el siguiente:
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'''
No se si hay una forma de crear las líneas a imprimir que sea mas fácil (por ahora es la que encontré), estoy recién empezando y no entiendo porque el bucle no funciona.