python - 为什么我在 csv 上的最后一个数据会被 python 删除
问题描述
我是 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)
解决方案
如果要附加到文件,则应更改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.
推荐阅读
- microsoft-graph-api - 随机开始得到400“无效请求”错误:“不支持提供的编码。”
- python - 如何在没有源代码的情况下获取网站的所有文本?
- flutter - 如何从 Native 平台获取回调事件到 dart 代码?
- azure-data-factory-2 - Azure 数据工厂 - 如何创建多个数据集并对同一 blob 容器中的文件应用不同的处理?
- javascript - 如何发送数据库内容而不是 [object Objet] Discord.js + mysql
- node.js - 如何将用户的消息作为变量?
- python - 在没有numpy的情况下计算非零值的数量
- go - Gorm 多对多关系重复值
- ms-access - 在 MS Access 或 Excel 中连接多个表
- python - 迁移后在 Django 上更改 db_table 不起作用,并且添加用户 (AbstractUser) 外键会破坏 __str__()