Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中使用csv阅读器搜索两个值的最佳方法_Python_File_Csv - Fatal编程技术网

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

在Python中使用csv阅读器搜索两个值的最佳方法,python,file,csv,Python,File,Csv,我有以下文件(bankdetails.txt) 以及以下旨在: 1.要求用户输入customerid和帐号(例如customer1和1),如果匹配文件详细信息,则打印“已授予访问权限”,否则打印“已拒绝” 代码如下。(注意?我不知道如何继续) 请回答以下问题 使用我的原始代码更正错误。解释访问字段时可能采取的措施(按索引) 介绍使用csv阅读器方法实现相同功能的任何简单方法 更新: 我知道字典是最合适的,但是我想用这些结构来解决这个问题 例如,下面的代码可以工作,但只适用于第一次迭代(custo

我有以下文件(bankdetails.txt)

以及以下旨在: 1.要求用户输入customerid和帐号(例如customer1和1),如果匹配文件详细信息,则打印“已授予访问权限”,否则打印“已拒绝”

代码如下。(注意?我不知道如何继续)

请回答以下问题

  • 使用我的原始代码更正错误。解释访问字段时可能采取的措施(按索引)
  • 介绍使用csv阅读器方法实现相同功能的任何简单方法
  • 更新: 我知道字典是最合适的,但是我想用这些结构来解决这个问题

    例如,下面的代码可以工作,但只适用于第一次迭代(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")         
    
    您可以尝试以下方法:

    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") 
    

    理想情况下,我不希望在教学中包含任何更复杂的内容……不使用字典或zip等。只使用行、字段和for循环……当然可以做得更简单?谢谢你的想法,你能评论一下你的代码吗。例如,不清楚什么是“用户”。。。。?请将其包含在整个代码的上下文中,以便我可以看到它是如何工作的。您从何处获取id?用户是在何处创建的?@pythoncarrot
    users
    是接收格式化csv文件的函数参数。谢谢-很有用,但如果没有函数,就不能仅使用行字段和for循环来完成此操作…这才是我真正想要的。可能重复No,这并不能回答我的问题,如果你真的阅读了这个问题(也是从我这里),你会注意到它处理的是一种完全不同的文件处理方法。(使用剥离和拆分,直接使用文件中的行)。这种方法特定于csv。谢谢@请参阅我在下面的编辑和说明。
    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")