【发布时间】:2018-09-19 14:40:21
【问题描述】:
所以我正在创建一个帐户登录系统,它在数据库中搜索用户名(及其相关密码),如果找到,将登录用户。
这是 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 匹配的任何想法
【问题讨论】:
-
for field in row搜索行中的每个字段。尝试查看特定列。例如。if row[0] == user_name: print("password is", row[1]). -
虽然它们写在同一行。就像它在 excel 上的同一领域一样。如果我将它添加到数组并搜索数组怎么办?不知道我会怎么做
-
我想你的意思是他们都在一个column?
-
我尝试使用 row[0]。还是同样的问题。仅在输入 ['dom', 'enter password'] 时才有效(这是电子表格/csv 文件中写入的字段的精确副本。
-
当你使用
row[0]时,你删除了for field in row:,对吧?