Parse txt file and return it in another file - Python [closed]

0

Good evening!

It has a file with numbers, saved in txt format, each number separated by space, there being a list with about 50 values in each line of the txt file (and more than 10,000 lines in the file): 78 34 85 67 96 197 etc.

I want to create a python program that compares 3 variables within the program with each line of the txt file. If the program reaches the end of all lines of the text file and does not find the 3 values, it will return the 3 search parameters in an output file, increment the variables in the loop and start over.

For example, the program starts running with variables 30 31 and 32. It will sweep line by line. If you reach the end of the last line of the text file and you have not found the 3 values, return those values in an output file and restarts the text file with the value of one of the incremented variables. If you find the 3 values before the end line of the file, stop the search, increment the variable and start the search in the first line of the file.

    
asked by anonymous 18.07.2018 / 02:12

1 answer

1

I solved the problem ...

To read the file, I used:     file_i = open (file_name, 'r')

I used the following statement to store the lines of the file (it will be necessary to read the same file several times):
    entradatxt = file_i.readlines ()

I have created 3 repetition structures using counters i, j, and k:

nums = [i,j,k]  # objeto com os 3 índices para comparação com a linha do 
                # arquivo txt
for linha in entradatxt:
    lista = linha.rstrip().split(" ") #atribuição da linha a uma lista
    lista = list(map(int, lista)) #conversão dos valores para int
                          # pois estava dando erro na set(nums) & set(lista)
    ocorrencias = set(nums) & set(lista) #comparação lista e índices
    if (len(ocorrencias) != 0):
        break

    if (k==300):
        aprumando = str(sorted(nums)) #para ordenar os índices)
        aprumando = aprumando +"\n" #quebra de linha
        file_o.write(aprumando) #adicionando no arquivo saída
file_o.close()         

Of course you have a way to simplify and improve execution, but this is what you're looking for ...

Thanks, guys!

    
19.07.2018 / 16:21