Reputation: 1170
My .csv
data looks like this:
June 8, 2009 Monday
June 8, 2009 Monday
June 6, 2009 Saturday
June 6, 2009 Saturday Correction Appended
June 6, 2009 Saturday
June 6, 2009 Saturday
June 6, 2009 Saturday
etc...
The data spans 10 years. I need to separate the months and years (and don't care about the dates and days).
To single out months I have the next lines of code:
for row in reader:
date = row[1]
month = date.partition(' ')[0]
print month
However I can't figure out how to extract the numeric year from the string? Would I have to use regex for this?
Upvotes: 0
Views: 92
Reputation: 2259
Try:
for row in reader:
row_split = row[1].split()
month = row_split[0]
year = int(row_split[3])
row[1] == "June 8, 2009 Monday"
Therefore:
row[1].split() == ["June", "8,", "2009", "Monday"]
So, your month and year are extracted as follows:
"June" == row[1].split()[0]
2009 == int(row[1].split()[2])
Upvotes: 5