为什么我在csv上的最后一个数据被python删除

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

我是python的新手,我正在CSV项目中进行一些写作但每次运行它都会删除我的上一个数据并写入一个新数据这是代码

with open('data.csv', 'w', newline='')as f:
    User_new_pass = input('enter your new password: ').strip()
    User_new_id = ' ' + input('enter your new user name: ').strip()
    User_new_info = [User_new_pass, User_new_id]
    linewriter = csv.writer(f)
    linewriter.writerow(User_new_info)
python python-3.x csv
1个回答
0
投票

如果要附加到文件,则应在open()功能中更改模式:

with open('data.csv', 'a', newline='')as f:
    # your code

[a-Open file in append mode. If file does not exist, it creates a new file.

[w-This Mode Opens file for writing. If file does not exist, it creates a new file. If file exists it truncates the file.

© www.soinside.com 2019 - 2024. All rights reserved.