I'm super new to Python and just got thrown for a loop when my source csv file changed its format. The date field now has this: 2020-07-22T00:00:00.000
when what I want is this: 2020-07-22
.
I need to read the source csv and append it to a database table. I looked at some other questions and found this sample code which I think will do what I want (create a copy of the csv with the newly formatted date which I can use with the rest of my code).
import csv
from datetime import datetime
with open(csvCasesVH, 'r') as source:
with open(csvCasesVH2, 'w') as result:
writer = csv.writer(result, lineterminator='\n')
reader = csv.reader(source)
source.readline()
for row in reader:
ts = datetime.strptime(row[0], '%Y-%m-%dT%H:%M:%S').strftime("%Y-%m-%d")
print(ts)
row[0]=ts
if ts != "":
writer.writerow(row)
source.close()
result.close()
I'm trying to figure out the datetime format - almost got there but now that this .000 hanging out there I don't know what to do with:
ValueError: unconverted data remains: .000
I would appreciate any advice. Thanks.