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

2024/11/16 4:43:29

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 faultuser_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

Answer

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 regexwith 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.

https://en.xdnf.cn/q/119570.html

Related Q&A

how to write a single row cell by cell and fill it in csv file

I have a CSV file that only has column headers:cat mycsv.csvcol_1@@@col_2@@@col_3@@@col_3I have to fill a single row with None values in each cell of the CSV file. Can someone suggest me the best-optim…

Greedy String Tiling in Python

I am trying to learn greedy string tiling in algorithmI have two lists as follows:a=[a,b,c,d,e,f] b=[d,e,a,b,c,f]i would like to retrieve c=[a,b,c,d,e]Another example would be a = [1,2,3,4,5,6,7,8,9,1,…

Python - efficient way to create 20 variables?

I need to create 20 variables in Python. That variables are all needed, they should initially be empty strings and the empty strings will later be replaced with other strings. I cann not create the var…

Whatsapp asking for updating chrome version

I am trying to open whatsapp with selenium and python, it was working fine until today. In headless or non, whatsapp is now asking to update chrome, when I try to do so, Chrome throws this error: An er…

how to find the longest N words from a list, using python?

I am now studying Python, and I am trying to solve the following exercise:Assuming there is a list of words in a text file, My goal is to print the longest N words in this list.Where there are several …

([False, True] and [True, True]) evaluates to [True, True]

I have observed the following behavior in python 3: >>> ([False, True] and [True, True]) [True, True]>>> ([False, True] or [True, True]) [False, True]I was expecting exactly the oppos…

Uploading an image to Flask server

I am struggling bit with Flask and uploading a file, here is my Flask code so far:@app.route(/api/user/update/, methods=[PUT]) @auth.login_required def update_user():# check if the post request has the…

Enemy Projectiles Arent Appending On Screen

I have here my script that targets the player what ever position he is at but the projectiles arent showing on my screen VIDEO. He isnt attacking at all, I dont know why. I am in my main loop I draw t…

Python+Selenium. Cant locate element

Ive implemented the script using Python and selenium to click on the ads. But now this script is not working.Unable to find element on the page.Please help me to correct the script. Thank you!from sele…

AttributeError: NoneType object has no attribute channels [duplicate]

This question already has answers here:Why do I get AttributeError: NoneType object has no attribute something?(11 answers)Closed 5 years ago.Hi Im having an issue with a module for my Discord bot. Im…