Python Forum

Full Version: Querying cvs excel files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I would like to find out how I could do query in csv excel file row by row. I don't want to use panda. If anyone know how to do it please share.

The code below does not find out the existing usernames and passwords in the file. When I open the file I can see the password and username that I am querying is there there but python cannot see it.

thanks.
        with open('Usernames_Passwords.csv', newline='') as myFile:  
            reader = csv.reader(myFile)
            for row in reader:
                if row==username and row==password:
                    print("Walid Username and Passwrod")
                else:
                    print("Wrong Inputs")
You need the proper indexes. Row is a list of all the comma separated walues in one line of the csv file. You need

if row[u] == username and row[p] == password:
where u is the index of the username column, and p is the index of the password column.
Thanks ichabod801. I tried but somehow it is not working.

        username=input("Enter Username: ")
        password=input("Enter Passwrod: ")


        with open('Usernames_Passwords.csv', newline='') as myFile:  
            reader = csv.reader(myFile)
            for row in reader:
                if row==username[1] and row==password[2]:
                    print("Valid Username and Password")
                else:
                    print("Wrong Inputs")
The outcome is always Wrong Inputs repeating itself for certain times.
You have the indexes ([1] and [2]) on the wrong things. They are on username and password, and are getting the second character of username and the third character of password. You want them on row, to get the second and third items in the list of values.
thanks. I have don it but no luck so far.

        username=input("Enter Username: ")
        password=input("Enter Passwrod: ")


        with open('Usernames_Passwords.csv', newline='') as myFile:  
            reader = csv.reader(myFile)
            for row in reader:
                if row[1] == username and row[2] == password:
                    print("Valid Username and Password")
                else:
                    print("Wrong Inputs")
The outcome is like:

Output:
Enter Username: mus34 Enter Passwrod: hello Wrong Inputs Wrong Inputs Wrong Inputs Wrong Inputs Wrong Inputs Wrong Inputs Wrong Inputs Wrong Inputs Wrong Inputs Walid Username and Passwrod
I have no idea of content of rows in file. Therefore I must guess: first word in row is username and second word is password. In this case correct indices are 0 and 1 as Python uses zero-based indexing.
It found the password. If you don't want it to print that every other password doesn't match, you need to change the logical structure. One way to do this is with a flag. Name a variable match_found, and set it to False before the loop. If the match is found, set it to True. Then check it after the loop, and if it is still False print the warning about invalid input.

Another way to do this is with the else statement for loops. An else statement after a loop executes the code under it if the loop ended without a break statement. So when you find the valid input, you break out of the loop. This saves you time checking the rest of the file once you know there's a match. Then you can put the warning about invalid input under the else statement, and it will only execute if not match is found (and thus no break is executed).