How to split comma delimited data and create a list from the data in python?
Question: I am trying to create a function that takes in two dates in YYYY/MM/DD format,
reads through the data, and returns a list of lists containing the latitude,
longitude, magnitude, and depth for the quakes between the two dates. The data
is in this format:

    
    
    Date,TimeUTC,Latitude,Longitude,Magnitude,Depth
    2012/02/23,08:09:13.0,-20.984,-178.654,4.6,526
    

This is my attempt:

    
    
    from tempBetweenDates import dateLessThan
    import urllib.request
    
    def betweenDates(date1, date2, date3): 
        """Determines if the first date is on the second or between the second and third date."""
        date_1 = date1.split('/')
        date_2 = date2.split('/')
        date_3 = date3.split('/')
        if int(date_1[0]) >= int(date_2[0]) and int(date_1[1]) >= int(date_2[1]) and int(date_1[2]) >= int(date_2[2]) and dateLessThan(int(date_1[1]), int(date_1[2]), int(date_1[0]), int(date_3[1]), int(date_3[2]), int(date_3[0])) == True:
        return True
    else:
        return False
    
    def parseEarthquakeData(date1, date2):
        page = urllib.request.urlopen("http://www.choongsoo.info/teach/mcs177-sp12/projects/earthquake/earthquakeData-02-23-2012.txt")
        eqdata = page.readlines()
        dataList = []
        for line in eqdata:
            lineSplit = line.split(',')
            date = lineSplit[0]
            data = lineSplit[2:6]
            dataList = [[data] for line in eqdata if betweenDates(date, date1, date2) == True]
        return(dataList)
    

Whenever I try and run the code I get an error:

    
    
    Traceback (most recent call last):
      File "<pyshell#2>", line 1, in <module>
        parseEarthquakeData("2012/02/22", "2012/02/19")
      File "C:\Users\lcooper2\Desktop\Python\PROJECTS\plotEarthquakes.py", line 20, in parseEarthquakeData
        lineSplit = line.split(',')
    TypeError: Type str doesn't support the buffer API
    

Any tips on how to avoid this error?


Answer: You can actually do something kind of cool with this! If you pipe your
response from the `urllib.request.urlopen` call through `csv.DictReader`, you
can eliminate a lot of your splitting and assigning.

    
    
    import csv
    import datetime
    import urllib.request
    
    page = urllib.request.urlopen("http://www.choongsoo.info/teach/mcs177-sp12/projects/earthquake/earthquakeData-02-23-2012.txt")
    reader = csv.DictReader((line.decode() for line in page), delimiter=',')
    
    for line in reader:
        # each line looks like:
        # {'Longitude': '-178.654', 'Date': '2012/02/23',
        #  'Depth': '526', 'Magnitude': '4.6', 'Latitude': '-20.984',
        #  'TimeUTC': '08:09:13.0'}
        # so you can use it like a dictionary!
        date = datetime.datetime.strptime(line['Date'], "%Y/%m/%d")
        # datetime objects like this aren't naive like numbers, so you can do:
        # datetime.datetime(year=2012, month=2, day=23) < datetime.datetime(year=2012, month=2, day=24)
        # and expect it to return True every time. This will massively simplify your
        # betweenDates function.
    

The cause of the error in your traceback is that `urllib.request.urlopen`
gives you an `HTTPResponse` object. That's an iterator that gives you `bytes`
objects, not `string` objects. Calling `bytes.decode()` turns them into
strings, so you can do stringy things with them like splitting them.

If you change to using these `datetime` objects, your `betweenDates` function
becomes:

    
    
    def between_dates(date1, date2, date3):
        return date2 <= date1 < date3
    

