我是Python新手,如果我的问题有点蠢的话,请原谅。我将一些数据发送到csv文件中(我正在制作一个密码管理器)。所以我把这个发送到这个文件(按这个顺序),网站的名称,相应的电子邮件,最后是密码。
但是我想打印已经写入csv文件中的所有名称,但这是我的问题,对于第一行,它确实打印了整行,但是对于下面的行,它工作得很好。
这是我的代码,希望你能帮我这个忙。
csv_file = csv.reader(open('mycsvfile.csv', 'r'), delimiter=';')
try :
print("Here are all the sites you saved :")
for row in csv_file :
print(row[0])
except :
print("Nothing already saved")也许这会有所帮助,但下面是我如何将数据写入csv文件的方法:
#I encrypt the email and the password thanks to fernet and an already written key
#I also make sure that the email is valid
file = open('key.key', 'rb')
key = file.read()
file.close()
f = Fernet(key)
website = input("web site name : \n")
restart = True
while restart :
mail = input("Mail:\n")
a = isvalidEmail(mail)
if a == True :
print("e-mail validated")
restart = False
else :
print("Wrong e-mail")
pws = input("password :\n")
psw_bytes = psw.encode()
mail_bytes = mail.encode()
psw_encrypted_in_bytes = f.encrypt(psw_bytes)
mail_encrypted_in_bytes = f.encrypt(mail_bytes)
mail_encrypted_str = mail_encrypted_in_bytes.decode()
psw_encrypted_str = psw_encrypted_in_bytes.decode()
f = open('a.csv', 'a', newline='')
tup1 = (website, mail_encrypted_str, psw_encrypted_str)
writer = csv.writer(f, delimiter = ';')
writer.writerow(tup1)
print("Saved ;)")
f.close()
return这是我的输出(我已经保存了数据) 输出(首先,您看到带有电子邮件和psw加密的ws的名称,然后就是我想要的名称。)
发布于 2022-08-05 17:35:57
我终于成功了,我没有使用csv.Reader,而是使用了csv.DictReader,而且由于我要查找的所有名称都在同一列上,所以我只需要使用列的标题。下面是代码:
with open('mycsv.csv', newline='') as csvfile:
data = csv.DictReader(csvfile)
print("Websites")
print("---------------------------------")
for row in data:
print(row['The_title_of_my_column'])发布于 2022-08-05 16:08:33
从csv.reader()生成列表
rows = [row for row in csv_file]现在您可以使用rows作为列表列表按标识符获取元素。
rows[id1][id2]https://stackoverflow.com/questions/73252516
复制相似问题