after creating a file (.txt) iterate from a line other than the start [closed]

0

It is the following I have an input file (.txt) which is as follows:

9
branco
preto
azul
verde
rosa
amarelo
vermelho
cinza
lilas
OHLEMREVU
BBRANCOZA
SRAMSUPAO
AABAPOTZZ
LNZROERNU
IUIEPDOII
LOLLORACA
ITMOTERPP
LIEAZVYUU

and from this I want to iterate only in the

OHLEMREVU
BBRANCOZA
SRAMSUPAO
AABAPOTZZ
LNZROERNU
IUIEPDOII
LOLLORACA
ITMOTERPP
LIEAZVYUU

For this I thought I used a for but whenever I do this it "selects" all the lines "instead of the intended part. Does anyone help me?

    
asked by anonymous 08.01.2017 / 18:24

1 answer

4

Assuming your first row has the number of rows to ignore (you did not make that clear), just do this:

try:
    file = open('teste.txt', 'r')

    # Lê o número de linhas a ignorar
    n = int(file.readline().strip())

    # Ignora as n-primeira linhas
    for i in range(n):
        file.readline()

    # itera sobre as demais linhas, como desejado
    for line in file:
        print(line.strip()) # O strip serve para remover a terminação de linha ('\n')
except:
    print('Oops! Não foi possível ler o arquivo de entrada.')
else:
    file.close()

Result:

> teste.py
OHLEMREVU
BBRANCOZA
SRAMSUPAO
AABAPOTZZ
LNZROERNU
IUIEPDOII
LOLLORACA
ITMOTERPP
LIEAZVYUU
    
08.01.2017 / 20:47