Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

python - Searching for only the first value in an array in a csv file

So i am creating a account login system which searches a database for a username (and its relevant password) and, if found, will log the user on.

This is what the csv file currently looks like

['dom', 'enter password']

This is written in one field (on an excel spreadsheet), and was written to the file when the user registered. However when I try to log on, the program will only log on if that is what is entered in the field, when I would like it to log on when dom is entered.

Here is the code which reads the csv file to see if the username/password is found on the file:

def Get_Details():
    user_namev2=user_name.get().lower() #Make it so entry box goes red if passwords password is incorrect, and red if username is incorrect/not fault
    user_passwordv2=user_password.get().lower()
    with open ('Accounts.csv', 'r') as Account_file:
        reader = csv.reader(Account_file)
        for row in reader:
            for field in row:
                if field == user_namev2:
                    print ("In file")

Here is how the username and password get written to the csv file upon registering an account.

if re.match(user_password2v2, user_passwordv2):
    print("Passwords do match")
    user_info = []
    user_info.append(user_namev2)
    user_info.append(user_passwordv2)
    with open ('Accounts.csv', 'a') as Account_file:
        writer = csv.writer(Account_file)
        writer.writerow([user_info])
        Bottom()

Any ideas on how i can search the csv file so that only a certain part of the string is searched and matched with user_namev2

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming the user_namev2is whatever is in the dom column and user_passwordv2 is whatever is in the enter password column, I would use regex to a part of the username that is in dom column.

import regex

with open ('Accounts.csv', 'r') as Account_file:
    reader = csv.reader(Account_file)
    for row in reader:
        if re.findall(user_namevv2[0:5],row[0]): #slice a part of the user name 
            print ("In file")

Note: I'm also choosing to take the first element of every row with row[0] since that is the dom column.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...