我的代码没有写入csv文件中的新行,而是写入我旁边的同一行

2024-09-30 20:27:32 发布

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

import csv 
with open ('database1.csv', 'a', newline='') as csvfile:
    linetowrite = username + "," + password + "," + dob + "," +\
                  favartist + "," + favgenre
    csvfile.write(linetowrite)
csvfile.close()

这是我的代码,所有的输入都可以,但似乎有什么问题,你能帮我吗


Tags: csvcsvfileimportaswithnewlineusernamepassword
2条回答

csvfile.write(linetowrite)更改为csvfile.write(linetowrite + "\n")。这将添加换行符

您正在导入csv模块,但从未实际使用它。为此,您需要创建如下^{}

import csv

with open('database1.csv', 'w', newline='') as csvfile:
    csvfilewriter = csv.writer(csvfile)
    csvfilewriter.writerow([username, password, dob, favartist, favgenre])

相关问题 更多 >