1

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?

1 Answer 1

4

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")
Sign up to request clarification or add additional context in comments.

2 Comments

Hum, got this error message when trying to run this : OSError: [Errno 22] Invalid argument: 'Output_2021-12-17T13:27:11Z.csv' Is it smthg wrong with my OS ?
@Amamra - I think yes, need remove : like df.to_csv(f"Output_{pd.to_datetime('now').strftime('%Y-%m-%dT%H%M%SZ')}.csv")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.