如何将Python密码导出到CSV?

2024-10-04 07:39:28 发布

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

我试图使用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)

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

screenshot

我希望输出看起来像:
enter image description here


Tags: csvcsvfile代码inimport密码forstring
1条回答
网友
1楼 · 发布于 2024-10-04 07:39:28

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

passwriter.writerow((password,))

相关问题 更多 >