I am reading a table and writing to CSV. I have some columns where I want to change the date format. Below is some code I have
for row in out[1]:
dt = datetime.datetime.strptime(row[18], '%Y-%m-%d %H:%M:%S.%f')
row[18] = dt.strftime('%Y-%m-%d)
print(row[18])
It does seem to change the date format to what I'm looking for because the print statement shows as much, but when I open the excel sheet that it wrote, the date format is like 11/12/2019 instead of 2020-11-12.
Any advice? I'm trying to prevent manually changing the format date once opening excel
Most likely, what Excel shows you is not what the underlying data is. Excel auto formats fields when it displays them. Hence, if it opens a CSV and sees 2020-11-12 it will recognize it is a date and display it for you as a formatted date field. If you want to save a field as TEXT that Excel will know is TEXT, then the cell has to start with a " ' ". Try adding this:
row[18] = dt.strftime('\'%Y-%m-%d')