仅搜索csv文件中数组中的第一个值 [英] 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.
这是csv文件当前的样子
This is what the csv file currently looks like
['dom','输入密码']
['dom', 'enter password']
此是在一个字段中(在excel电子表格上)编写的,并在用户注册时写入文件中。但是,当我尝试登录时,只有在输入了dom的情况下我希望它登录时,程序才会登录。
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.
下面是读取csv文件以查看是否在文件中找到用户名/密码的代码:
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")
这是在注册帐户后如何将用户名和密码写入csv文件。
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()
关于如何搜索csv文件的任何想法,以便仅搜索字符串的特定部分并与<$ c $匹配c> user_namev2
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
推荐答案
假设 user_namev2
是 dom
列中的内容,而 user_passwordv2
是输入密码中的内容
列,我将对 dom
列中的用户名
的一部分使用正则表达式。
Assuming the user_namev2
is 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")
注意:我也选择接受具有 row [0]
的每个 row
的第一个元素,因为那是 dom
列。
Note: I'm also choosing to take the first element of every row
with row[0]
since that is the dom
column.
这篇关于仅搜索csv文件中数组中的第一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!