How to print line number 10 after each special character '\ x0c' found in the file with Python

0

Good, I have the following text

'\x0c' texto largo largo
10 lineas despues 
900900
texto largo texto largo
'\x0c' texto largo largo
10 lineas despues 
900901
texto largo texto largo
'\x0c' texto largo largo
10 lineas despues 
900906
texto largo texto largo

I need to print those numbers that are 10 lines below the '\ x0c' always

what I did until now print the line number directly but it is impossible for me to know all

with open("archivolargo.txt") as f:
    for i, line in enumerate(f):
        if i == 10:
            print line[0:5]
        if i == 180:
            print line[0:5]
        if i == 297:
            print line[0:5]

thanks for your help

    
asked by Giacomo F 17.10.2018 в 23:11
source

2 answers

0

It occurs to me to count the lines of the file, and restart that counter every time a line begins that starts with \x0c .

When the counter is 10, the number in question is printed.

If the file does not start with \x0c , a special precaution must be taken to avoid printing line 10 of the file. The simplest thing is to start the line counter with 11 so that it can not happen.

c = 11

for linea in fichero:
  c+=1
  if c == 10:
    print linea[0:5]
  if linea.startswith("\x0c"):
    c=0
    
answered by 18.10.2018 в 08:52
0
contador = 0
caracter_encontrado = False
archivo = open("archivo.txt")
valores = []

for linea in archivo:
    if(linea.find("\x0c") != -1):
        caracter_encontrado = True 
    if(caracter_encontrado):
        contador += 1
        if(contador == 12):
            valores.append(linea)
            caracter_encontrado = False
            contador = 0

archivo.close()

print("\n".join(valores))
    
answered by 21.10.2018 в 06:42