0

trying to implement nested "for" loop in CSV files search in way - 'name' found in one CSV file being searched in other file. Here is code example:

    import csv
    import re

 
# Open the input file 
with open("Authentication.csv", "r") as citiAuthen:
    with open("Authorization.csv", "r") as citiAuthor:
    #Set up CSV reader and process the header
        csvAuthen = csv.reader(citiAuthen, quoting=csv.QUOTE_ALL, skipinitialspace=True)
        headerAuthen = next(csvAuthen)
        userIndex = headerAuthen.index("'USERNAME'")
        statusIndex = headerAuthen.index("'STATUS'")

        csvAuthor = csv.reader(citiAuthor)
        headerAuthor = next(csvAuthor)
        userAuthorIndex = headerAuthor.index("'USERNAME'")
        iseAuthorIndex = headerAuthor.index("'ISE_NODE'")


  
        # Make an empty list
        userList = []
        usrNumber = 0
        # Loop through the authen file and build a list of 
        for row in csvAuthen:
            user = row[userIndex]
            #status = row[statusIndex]
            #if status == "'Pass'" :
            for rowAuthor in csvAuthor:
                userAuthor = rowAuthor[userAuthorIndex]
                print userAuthor

What is happening that "print userAuthor" make just one pass, while it has to make as many passes as there rows in csvAuthen. What I am doing wrong? Any help is really appreciated.

1 Answers1

0

You're reading the both files line-by-line from storage. When you search csvAuthor the first time, if the value you are searching for is not found, the file pointer remains at the end of the file after the search. The next search will start at the end of the file and return immediately. You could need to reset the file pointer to the beginning of the file before each search. Probably better just to read both files into memory before you start searching them.

Nicholas Hunter
  • 1,791
  • 1
  • 11
  • 14
  • Thanks Nicholas. I am just staring Python so not very fluent. can you, please, provide an example how to to read it in memory ? – Gennady Yakubovich Apr 29 '21 at 13:49
  • Maybe this will help [How do I read and write CSV files with Python?](https://stackoverflow.com/questions/41585078/how-do-i-read-and-write-csv-files-with-python) – Nicholas Hunter Apr 29 '21 at 13:55