For nested only works in first iteration (Python)

Asked

Viewed 108 times

1

I’m trying to get one nested to compare lines in two different csv files, but after the first interaction of the first file is searched inside the second file and skip to the second iteration, the for2 stops running and only the for1 continues. Would anyone know why?

def evolution(token, evolution_results):
    files_name = open("file_name_results/" + token + "_match_file.csv")
    results = open("results/" + token + "_results.csv")

    for line1 in files_name:
        print("Inicio iteração")
        line1 = line1.replace("\n","")
        if line1=="file" or line1=="":
            print("Fim iteração")
            continue
        print("Buscando "+line1)
        for line2 in results:
            line2 = line2.replace("\n","")
            if re.search('(=?' + ''.join(line1).strip() + ')', line2, re.IGNORECASE):
                # evolution_results.write(line2+"\n")
                print(line2)
        print("Fim iteração")
    print("Saiu do for")            
    files_name.close()
    results.close()

Obs: in the for1 file the first two lines will always be skipped

Exit

Start iteration End iteration Start iteration End iteration
Home iteration Searching github_repos/Bancor/solidity/Contracts/Safemath.sol
c527907d648afdee3aa4f9ad2d6c44c5fdb2130a,github_repos/Bancor/solidity/Contracts/Safemath.sol,99 317c0702eefe3909cbbaa53094340292a39c0ad4,github_repos/Bancor/solidity/Contracts/Safemath.sol,99 b3935bb97eea724fd536ce87deaff53629e5aa25,github_repos/Bancor/solidity/Contracts/Safemath.sol,99 3ffe6c9edb11b506817587ca5ab8b9913abd87d0,github_repos/Bancor/solidity/Contracts/Safemath.sol,99 34d71ebe3777a2d9e0650b2e1f462def0e0096f0,github_repos/Bancor/solidity/Contracts/Safemath.sol,99 12dd35e3d80115e23893a4ad07b0d6edc9f25403,github_repos/Bancor/solidity/Contracts/Safemath.sol,99 85de77f365bc4fb8b269c45e983cc3a2cc5823d3,github_repos/Bancor/solidity/Contracts/Safemath.sol,99 1cfb8d0957462a7f7fe289471b9e7863d32e2aab,github_repos/Bancor/solidity/Contracts/Safemath.sol,100 b1137aaa2536973339e8e4cd07d1129eefc0879c,github_repos/Bancor/solidity/Contracts/Safemath.sol,100 7d2956a7a2b4209a0620ed0be1aa8be16f210cb6,github_repos/Bancor/solidity/Contracts/Safemath.sol,100 ce4b8dac9a0b5a76c5f09bd94ffe6f86f8dc9c62,github_repos/Bancor/solidity/Contracts/Safemath.sol,100 End iteration Start iteration Searching github_repos/Bancor/solidity/Contracts/Ierc20token.sol End iteration Home iteration Searching github_repos/Bancor/solidity/Contracts/Iowned.sol End iteration
Home iteration Searching github_repos/Bancor/solidity/Contracts/Smarttoken.sol End iteration
Came out of the closet

  • You know what generators are in Python?

  • No, I’m new to the language

1 answer

3


The function open returns a file object. When we read the contents of this object, or iterate over it line by line as you do in your code, we arrive at the "end" of the file. At this point, any other operation will return an empty string:

>>> arquivo = open('texto.txt')
>>> arquivo.read()
'olá mundo'
>>> arquivo.read()  # já cheguei ao final do arquivo
''

In your case, the problem is that the loop for line2 in results: goes through the entire open archive results the first time it executes, and in the following executions this loop is not executed (a loop over an empty sequence returns immediately in Python, for example for caractere in '': print(caractere)).

You have some options to get around this problem:

1) Store the contents of open files in lists

Replacing the first lines by:

files_name = open("file_name_results/" + token + "_match_file.csv").readlines()
results = open("results/" + token + "_results.csv").readlines()

You will read the entire files in lists of strings, which can be iterated as many times as you want. The rest of your code should work the way it is.

2) Calling out the method seek of the open file

You can use the method seek of a file object to return to the top of it:

>>> arquivo = open('texto.txt')
>>> arquivo.read()
'olá mundo'
>>> arquivo.read()  # já cheguei ao final do arquivo
''
>>> arquivo.seek(0)  # voltando ao começo do arquivo
0
>>> arquivo.read()
'olá mundo'

That way you can call the method results.seek(0) at the end of its nested loop to "restore" the initial condition of the opened file.

  • 1

    Thank you :) I really liked your explanation and helped to solve my problem

Browser other questions tagged

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