Home > Enterprise >  Custom csv file name using to_csv Python
Custom csv file name using to_csv Python

Time:12-18

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

Any idea?

CodePudding user response:

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")
  • Related