仅搜索 csv 文件中数组中的第一个值



所以我正在创建一个帐户登录系统,该系统在数据库中搜索用户名(及其相关密码(,如果找到,将登录用户。

这是 csv 文件当前的样子

["DOM", "输入密码"]

这写在一个字段中(在 Excel 电子表格上(,并在用户注册时写入文件。但是,当我尝试登录时,程序只会在字段中输入的内容时登录,当我希望它在输入 dom 时登录时。

以下是读取csv文件以查看是否在文件上找到用户名/密码的代码:

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文件的方式。

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文件以便仅搜索字符串的某一部分并与user_namev2匹配的任何想法

假设user_namev2dom列中的任何内容,user_passwordv2enter password列中的任何内容,我将对dom列中的username的一部分使用正则表达式。

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的第一个元素与row[0]一起使用,因为这是dom列。

最新更新