简体   繁体   English

如何使用 pandas 由 python 更新而不是重写 csv?

[英]How to update instead of rewriting csv by python with pandas?

Every day I'm scraping a CSV file from the web and updating a CSV file in my system.每天我都在从 web 中抓取 CSV 文件,并在我的系统中更新 CSV 文件。 How do I update the CSV file?如何更新 CSV 文件? I don't want to create a new CSV file every day, I just need to update one CSV file?.我不想每天都创建一个新的 CSV 文件,我只需要更新一个 CSV 文件? Before that, I need to remove previous days data so that I don't have duplicate data in CSV.在此之前,我需要删除前几天的数据,以免 CSV 中的数据重复。

 f = open("data.csv", "w+") # deletes the old content in your csv file (all content) f.close() your_work=True while your_work: new_df=pd.read_csv("new.csv") # add your new datas from web or file etc new_df.to_csv('data.csv', mode='a', header=False) #append mode("a") saves the df data to your csv file line by line your_work=False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM