My python code shown below is not retrieving any dates before the year 1900. I learned that this is a limitation of datetime.strptime()
.
I have tried to follow some of the workaround mentioned in similar posts , Is there any way to use a strftime-like function for dates before 1900 in Python? but they seem a bit complicated for me to follow. I also learned that there is a patch available to fix this issue. https://bugs.python.org/file10253/strftime-pre-1900.patch
I tried to install the patch in Pycharm by copying the patch to a text file, but I am getting the following error message. Any ideas on what I need to do to run the patch successfully to get dates before 1900 ?
Pycharm Patch Error Screenshot
My Code:
from datetime import datetime
import csv
with open('train.csv', 'r') as f_input, open('sample.txt', 'w') as f_output:
csv_input = csv.reader(f_input)
csv_output = csv.writer(f_output)
for row in csv_input:
for date_format in ['%Y']:
try:
converted = datetime.strptime(row[3], date_format)
csv_output.writerow([row[0], row[1], row[2], converted.strftime(date_format)])
except ValueError:
pass