Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Custom csv file name using to_csv Python

I’m working on writing a pandas DataFrame to a CSV file using to_csv :

df.to_csv("Output.csv")

I’m looking to customize the naming of the csv file by adding the actual datetime so it should look like this : Output_2021-12-17T14:09:09Z

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any idea?

>Solution :

Use f-strings with to_datetime and Timestamp.strftime:

print (f"Output_{pd.to_datetime('now').strftime('%Y-%m-%dT%H:%M:%SZ')}.csv")
Output_2021-12-17T13:12:58Z.csv

df.to_csv(f"Output_{pd.to_datetime('now').strftime('%Y-%m-%dT%H:%M:%SZ')}.csv")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading