Read from CSV file and make plot
Question: I have a little problem I hope someone could help me with. I'm not the best at
python.

I have a "CSV" file that I have to manipulate. I have 3 questions I hope you
could help with.

**1: Print the first two lines**

The first I think I done already, I printed the first two lines.

    
    
    import csv
    from pprint import pprint
    
    data = open('iphonevsandroid.csv')
    
    pprint (data.readlines(2))
    f.close()
    

I getting data like this:

    
    
    ['week,iphone,android\n',
     '2004-01-04 - 2004-01-10,0,0\n',
     '2004-01-11 - 2004-01-17,0,0\n',
     '2004-01-18 - 2004-01-24,0,0\n',
     '2004-01-25 - 2004-01-31,0,0\n',
     '2004-02-01 - 2004-02-07,0,0\n',
     '2004-02-08 - 2004-02-14,0,0\n',
     '2004-02-15 - 2004-02-21,0,0\n',
     '2004-02-22 - 2004-02-28,0,0\n',
     '2004-02-29 - 2004-03-06,0,0\n',
     '2004-03-07 - 2004-03-13,0,0\n',
     '2004-03-14 - 2004-03-20,0,0\n',
    

**2: Parse the first field to convert it to a single date object (hint: use
datetime.strptime). You can choose any of the two dates.**

    
    
    import csv
    import datatime
    
    data = open('iphonevsandroid.csv')
    
    reader1 = csv.reader1(data)
    
    for row in reader1:
        print row[0]
    

This will print the first row we needed, but how do I get 1 of the dates? I
have to plot it later.


Answer: I would use the string.split method.

In your case, you can split by ' - '

So if:

    
    
    row = ['week,iphone,android\n',
    '2004-01-04 - 2004-01-10,0,0\n',
    '2004-01-11 - 2004-01-17,0,0\n',
    '2004-01-18 - 2004-01-24,0,0\n',
    '2004-01-25 - 2004-01-31,0,0\n',
    '2004-02-01 - 2004-02-07,0,0\n',
    '2004-02-08 - 2004-02-14,0,0\n',
    '2004-02-15 - 2004-02-21,0,0\n',
    '2004-02-22 - 2004-02-28,0,0\n',
    '2004-02-29 - 2004-03-06,0,0\n',
    '2004-03-07 - 2004-03-13,0,0\n',
    '2004-03-14 - 2004-03-20,0,0\n']
    

We can take the first time value as:

    
    
    first_date_row = row[1]
        Output: '2004-01-04 - 2004-01-10,0,0\n'
    

You see that the first date is separated from the second one by ' - '.

so:

    
    
    first_date = first_date_row.split(' - ')[0]
    Output: '2004-01-04'
    

How do we get the second date? Well we can do a similar procedure than we did
with the first date, but taking the first comma after the second date as the
separator.

Thus:

    
    
    second_date = first_date_row.split(' - ')[1].split(',')[0]
    Output: '2004-01-10'
    

Please note, these dates are just text for Python, you will need to transform
them into date objects. You could use in this case:

    
    
    first_date_formated = datetime.strptime(first_date, '%Y-%m-%d')
    second_date_formated = datetime.strptime(second_date, '%Y-%m-%d')
    

