Created
February 27, 2025 08:16
-
-
Save tomasruizt/b1c7d595daf3feeae5a78a0043c2965e to your computer and use it in GitHub Desktop.
Incremental CSV saving
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import pandas as pd | |
import time | |
csv = "file.csv" | |
for i in range(10): | |
row = pd.DataFrame([{"time": pd.Timestamp.now(), "data": i}]) | |
time.sleep(0.5) | |
if i == 9: | |
raise Exception("Some error at the end!") | |
add_header: bool = not os.path.exists(csv) | |
row.to_csv(csv, mode="a", header=add_header, index=False) | |
print(f"Saved row {i}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DataFrame.to_csv()
to write chunks of data incrementally to a CSV in a for loop.i=9
.