

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
Not enough gems
Cost: 6 gems
1: Data Formats
incomplete
2: Parsing JSON
incomplete
3: Variable-Depth JSON
incomplete
4: Fetching JSON
incomplete
5: CSV Files
incomplete
6: CSV Type Conversion
incomplete
7: Filtering CSV Rows
incomplete
8: Writing CSV Files
incomplete
9: Appending to CSV Files
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Sometimes you want to create a brand-new CSV file, but sometimes you want to keep the existing file and append more rows to the end.
If you open a file in "a" (append) mode, Python appends new content instead of overwriting the file:
with open("posts.csv", "a", newline="") as file:
writer = csv.DictWriter(file, fieldnames=["post_id", "likes", "published"])
writer.writerow({"post_id": "post-12", "likes": "350", "published": "true"})
When appending, don't call writeheader() again unless you're creating a brand-new file. You could wind up with duplicate headers in the middle of your CSV:
post_id,likes,published
post-01,1200,true
post_id,likes,published
post-12,350,true
The ops team has an existing CSV file with device data. You need to append new readings without overwriting what's already there.
Complete the append_multiple_readings function. It accepts a list of device dictionaries and appends them to an existing CSV file at the provided path.