在Python中使用csv reader搜索两个值的最佳方法

2024-04-26 10:45:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下文件(银行详情.txt)你知道吗

customer1,1
customer2,2
customer3,3
customer4,4
customer5,5

以下代码旨在: 1要求用户输入customerid和帐号(例如customer1和1),如果匹配文件详细信息,则打印“access granted”,否则打印“denied”。你知道吗

代码如下。(注意?我不知道如何进行)

def read_from_file_csv_method2():
   accessgranted=False
   while accessgranted==False:
      with open("bankdetails.txt","r") as f:
         reader=csv.reader(f)
         for row in reader:
            for field in row:
               username=input("uesrname:")
               accountno=input("account no:")
               if username==field and accountno==?:
                  accessgranted=True
                  break
               else:
                  accessgranted=False

         if accessgranted==True:
            print("Access Granted")
         else:
            print("Sorry, wrong credentials")       

请回答以下问题

  1. 使用我的原始代码更正错误。解释访问字段(按索引)可能采取的措施
  2. 使用csv reader方法提供任何简单的方法来实现相同的目标

更新: 我知道字典是最合适的,但是我想用这些结构来解决这个问题。你知道吗

例如,下面的代码只适用于第一次迭代(customer1和1)…不适用于其他任何东西。你知道吗

 if username==row[0] and accountno==row[1]:

在下面的代码中使用了上面的代码…但是仍然不能正常工作

def read_from_file_csv_method2():
   accessgranted=False
   while accessgranted==False:
      with open("bankdetails.txt","r") as f:
         reader=csv.reader(f)
         for row in reader:
               username=input("uesrname:")
               accountno=input("account no:")
               if username==row[0] and accountno==row[1]:
                  accessgranted=True
                  break
               else:
                  accessgranted=False

         if accessgranted==True:
            print("Access Granted")
         else:
            print("Sorry, wrong credentials")         

Tags: csv代码txtfalsetrueinputifusername
1条回答
网友
1楼 · 发布于 2024-04-26 10:45:01

你可以试试这个:

import csv

data = csv.reader(open('filename.txt'))
username=input("uesrname:")
accountno=input("account no:")
new_data = [{a:b for a, b in zip(["name", "id"], i)} for i in data]
if any(i["name"] == username and i["id"] == accountno for i in new_data):
    print("Access Granted")
else:
    print("Sorry, wrong credentials")  

更简单的方法包括and函数和for循环:

def check_credentials(users, username, id):
   for user, the_id in users: #here, unpacking from list. user is the first value in the list that is created via iteration.
       if user == username and the_id == id:
             return True #return will break out of the for loop
   return False #if the loop has not been broken out of at the end of the iteration, it will return False

data = csv.reader(open('filename.txt'))
username=input("uesrname:")
accountno=input("account no:")
if check_credentials(data, username, accountno):
    print("Access Granted")
else:
    print("Sorry, wrong credentials")

没有函数:

flag = False
for user, the_id = data: #data is the csv file. 
    if user == username and the_id == accountno:
         flag = True
         break
if flag:
     print("Access Granted")
else:
     print("Sorry, wrong credentials") 

相关问题 更多 >