Created
October 13, 2022 04:46
-
-
Save BlueSideStrongSide/7601561cf851fd6f68147f4fdd7eaf88 to your computer and use it in GitHub Desktop.
simpl csv out
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 csv | |
import os | |
# input arguments your track dict | |
# input arguments the filename of the newly created csv | |
def _export_to_csv(input_dict: dict = None, export_filename: str = None): | |
try: | |
# example.csv is the name of our exported file | |
# mode = a+ we open a file handled in append plus mode | |
with open(export_filename, mode="a+", newline='') as out_csv: | |
writer = csv.DictWriter(out_csv, fieldnames=input_dict.keys()) | |
# checking file size to determine if we need a header row | |
if os.stat(export_filename).st_size == 0: | |
writer.writeheader() | |
# writing the data in your dict to the csv | |
writer.writerow(input_dict) | |
return "Success" | |
# catch any exceptions and print them to the terminal | |
except Exception as e: | |
print(e) | |
return "Failure" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment