【问题标题】:How to delete only one row of a csv and save the changes in the same csv? [Python]如何仅删除 csv 的一行并将更改保存在同一个 csv 中? [Python]
【发布时间】:2020-03-21 12:36:06
【问题描述】:

我正在用 Python 制作登录脚本。 在此脚本中,如果您多次输入错误密码,您的帐户将被锁定。 您的用户名被放入名为“lockedaccounts.csv”的 csv 文件中 一旦用户输入他们的恢复密钥,该帐户就会被解锁。 一旦他们输入了恢复密钥,我需要从 lockedaccounts.csv 中删除用户名。

这是我当前的代码

                   for k in range(0, len(col0)):
                        if col0[k] == username_recovery and col1[k] == recoverykeyask:
                            messagebox.showinfo('Account Recovery Successful', "Your account has been recovered.")
                            break
                        else:
                            accountscanningdone = False
                    else:
                        accountrecovered = False

感谢您的宝贵时间。

【问题讨论】:

    标签: python csv authentication


    【解决方案1】:

    最简单的代码是这样的:

    import csv
    
    #when you want to add
    with open('lockedaccounts.csv', 'w', newline='') as file:
        writer = csv.writer(file)
        writer.writerow(["username2"])
    
    #when you want to delete
    rows = []
    with open('lockedaccounts.csv', 'r') as csvfile: 
        csvreader = csv.reader(csvfile) 
        for row in csvreader: 
            rows.append(row) 
    
    rows.remove(['username2'])
    
    with open('lockedaccounts.csv', 'w') as file:
        writer = csv.writer(file)
        writer.writerows(rows)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多