Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
写入下一行python csv_Python_Csv - Fatal编程技术网

写入下一行python csv

写入下一行python csv,python,csv,Python,Csv,我正在做一个项目,我认为需要使用CSV文档之类的东西来存储数据。但我遇到了一个问题,我需要python每次都将变量写入下一行,而不是第一行(这将覆盖该列中的现有数据)。谁能帮我解决我的问题 这是我的密码: import csv tag=input('Enter label for the new profile: ') pwd=input('Type password for the new profile: ') b=open('database.csv', 'w',) a=csv.write

我正在做一个项目,我认为需要使用CSV文档之类的东西来存储数据。但我遇到了一个问题,我需要python每次都将变量写入下一行,而不是第一行(这将覆盖该列中的现有数据)。谁能帮我解决我的问题

这是我的密码:

import csv
tag=input('Enter label for the new profile: ')
pwd=input('Type password for the new profile: ')
b=open('database.csv', 'w',)
a=csv.writer(b)
a.writerow([tag,pwd])
b.close()

提前感谢。

您希望在写入时使用“附加”模式

在打开的“a”中更改“w”模式

更改此项:

b=open('database.csv', 'w',)
为此:

b=open('database.csv', 'a',)
阅读有关不同模式的文档

“w”仅用于写入(将使用具有相同名称的现有文件 “a”打开文件进行附加;写入数据库的任何数据 文件将自动添加到末尾