after creating a file(.txt) iterate from a line that is not the beginning

Asked

Viewed 258 times

1

The following I have an input file(.txt) which is the following:

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 on the part

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 lines" instead of the intended part. Someone help me?

  • 1

    Hello, welcome to SOPT. Your question is not clear. Are the words in the file all together, or one on each line? How you made your code (edit the question and post the relevant excerpt). If not yet, do the [tour] and mainly read [Ask].

  • Either way, you’re gonna have to iterate line by line. It takes the value of the first (which I suppose is how many words are) and puts it in a list (with an if counter < number of the first line). If the IF condition has been reached, what comes next is your word search board. In short: take 1st line, convert to numeric. You take the following lines and increment a counter. for as long as the counter is less than or equal to the initial value, store in the word list, otherwise store on the board.

  • Yes, the file is as it is now (every word per line).

  • Toshiyuki, it would be nice if you click [Edit] and add the code you tried in the post, it is easier for those who respond to use as a basis to locate the problem or propose solution..

  • Just one more question if I want in tuples(tuples) instead of lists as I would do?

  • I’m still at the beginning but when I’m done doing the search function.

  • @Toshiyuki posts that initial part in the question, so the staff can post an official answer. Thus, it solves this initial part, then when searching opens a separate question from the already solved code. The ideal here is always to divide the problem into small parts, and solve one at a time in each question, objectively. Click on [Edit] and put the code, even if it still doesn’t work as you want, and explain in more detail, then the chance of a legal answer increases, instead of us solving in the comments.

  • It would be nice if in the question you made it clear that these are crossword puzzles.

Show 3 more comments

1 answer

5


Assuming your first line has the number of lines to ignore (you didn’t make that exactly clear), just do so:

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()

Upshot:

> teste.py
OHLEMREVU
BBRANCOZA
SRAMSUPAO
AABAPOTZZ
LNZROERNU
IUIEPDOII
LOLLORACA
ITMOTERPP
LIEAZVYUU
  • @Miguel Well noted Miguel. Thank you. I made the suggested correction. :)

  • 1

    You’re welcome Luiz, good answer

  • 1

    +1, I believe that for what was asked, it is a solution that shows all the details involved. I believe that for the next steps (such as the tuple question), the author can ask separate and specific new questions if he needs help from the community, starting from this solution after adjusting to his real case. Even, with this answer he can already get the list of words in the first for and the "board" in the second

Browser other questions tagged

You are not signed in. Login or sign up in order to post.