Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Querying cvs excel files
#1
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")
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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
Reply
#6
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy Paste excel files based on the first letters of the file name Viento 2 425 Feb-07-2024, 12:24 PM
Last Post: Viento
  How to loop through all excel files and sheets in folder jadelola 1 4,462 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  Basic SQL query using Py: Inserting or querying sqlite3 database not returning data marlonbown 3 1,363 Nov-08-2022, 07:16 PM
Last Post: marlonbown
  Creating csv files from Excel file azizrasul 40 5,583 Nov-03-2022, 08:33 PM
Last Post: azizrasul
  Working with excel files arsouzaesilva 6 3,152 Sep-17-2021, 06:52 PM
Last Post: arsouzaesilva
  win32com — How to resolve “AttributeError: xlUp” for Excel files? JaneTan 2 4,217 Aug-18-2021, 05:27 AM
Last Post: snippsat
  code to read files in folders and transfer the file name, type, date created to excel Divya577 0 1,857 Dec-06-2020, 04:14 PM
Last Post: Divya577
  Creating Excel files compatible with microsoft access vkallavi 0 1,587 Sep-17-2020, 06:57 PM
Last Post: vkallavi
  Merging Excel Files JezMim 1 1,896 Sep-06-2020, 08:56 PM
Last Post: bowlofred
  Fastest Method for Querying SQL Server with Python Pandas BuJayBelvin 7 6,878 Aug-02-2020, 06:21 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020