Reputation: 1470
I'm reading in a series of CSV files then writing them back out. Some files have different date formats, but I want to write them all out in "%Y-%m-%d"
format (YYYY-MM-DD).
How can I write some if statements for the case when the date format is "%m/%d/%Y"
and for "%m-%d-%Y"
?
Here's what I've attempted :
reader = csv.reader(gzip.open(readfile), delimiter=',')
for line in reader:
if datetime.datetime.strptime(line[10], "%m/%d/%Y") == "%m/%d/%Y"
line[10] = datetime.datetime.strptime(line[26], "%m/%d/%Y").strftime("%Y-%m-%d")
else:
line[10] = datetime.datetime.strptime(line[26], "%m-%d-%Y").strftime("%Y-%m-%d")
Upvotes: 0
Views: 105
Reputation: 5301
You can use the python dateutil
lib. It can do this automatically so that you do not need to write if
statements at all.
>>> from dateutil.parser import parse
>>> parse("10/03/2017")
datetime.datetime(2017, 10, 3, 0, 0)
>>> parse("03-12-2010")
datetime.datetime(2010, 3, 12, 0, 0)
This only works if your month always comes first (but this is true for if
statements as well). You could also call parse
with the option dayfirst=True
, if your format were e.g. "%d/%m/%Y"
, i.e. your day always came first.
Combined with your example this would mean:
from dateutil.parser import parse
for line in reader:
line[10] = parse(line[26]).strftime("%Y-%m-%d")
(PS: If you are not 100% sure that your line always actually has a valid date string, you should probably include ValueError
handling in case parse
cannot undestand the string you passed to it.)
Upvotes: 1
Reputation: 1162
According to Python's EAFP Philosophy.
If you are certain that your CSV only contains dates in those two formats you can try doing this.
for line in reader:
try:
line[10] = datetime.datetime.strptime(line[26], "%m/%d/%Y").strftime("%Y-%m-%d")
except ValueError:
line[10] = datetime.datetime.strptime(line[26], "%m-%d-%Y").strftime("%Y-%m-%d")
Upvotes: 1