如何将Python秘密密码导出到CSV?

问题描述 投票:0回答:1

我正在尝试使用Python的Secrets模块创建x个密码,然后将其导出到新的CSV文件中。我可以创建密码并将其打印在屏幕上,但是当我写入CSV时,字符会以逗号解析。 (对不起,代码未显示为正确屏蔽)

代码:

import secrets
import string
import csv

need = input("Number of passwords:")
need = int(need)
alphabet = string.ascii_letters + string.digits

with open('password.csv', 'w', newline='') as csvfile:
    passwriter = csv.writer(csvfile, delimiter=',')
    for n in range(need):
        password = ''.join(secrets.choice(alphabet) for i in range(10))
        print(password)
        passwriter.writerow(password)

当前,我得到的输出不是我想要的:

“截图”

python csv
1个回答
0
投票

将您的string解释为值列表,将其转换为一个值的元组,如下所示:

passwriter.writerow((password,))
© www.soinside.com 2019 - 2024. All rights reserved.