Python - How to write new data after creating new csv files?

Asked 2 years ago, Updated 2 years ago, 53 views

For example, I want to measure the price change for a specific item every time the program runs by time and save it as a file. If we do it as below, the existing contents of output.csv will disappear, so what should we do?

import csv
f = open('output.csv', 'w', encoding='utf-8', newline='') writing = csv.writer(f) writing.writerow ("Content") f.close()

python csv backup

2022-09-22 13:49

1 Answers

https://docs.python.org/3/library/functions.html#open

Look at the options of open in the above document.

'a' open for writing, appending to the end of the file if it exists
f = open('output.csv', 'a', encoding='utf-8', newline='')

You can do it.


2022-09-22 13:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.