0

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?

3
  • If your month is June 6, then you can get 6 by month.split(" ")[1] Commented Jun 9, 2015 at 10:45
  • @TheMonk OP wants to extract "the numeric year" Commented Jun 9, 2015 at 10:58
  • @OhAuth Was in a hurry and glanced the same. Good catch. _/ Commented Jun 9, 2015 at 13:17

1 Answer 1

5

Try:

for row in reader:
    row_split = row[1].split()
    month = row_split[0]
    year = int(row_split[3])

Explaination

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])
Sign up to request clarification or add additional context in comments.

6 Comments

Shouldn't both be [0]?
@bereal There is a space at the start of the second column. If OPs data is truly comma separated this adds a blank string at the start of the split array. See the edit explanation.
' a b c '.split() returns ['a', 'b', 'c'], unlike ' a b c '.split(' ')
@bereal I originally had split(' '), however, a suggested edit changed it to split(). Needless to say I've fixed the issue from the suggested edit in my answer. +1 for the tip.
Thanks for the explanation, but actually the data format I provided above is already extracted from row[1] in the .csv. So row[1] contains the full string e.g., June 6, 2009 Saturday Correction Appended.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.