简体   繁体   中英

Python: Trouble with Decode(ascii)

I'm supposed to write a function that does the following

Write the contract, docstring, and implementation for a procedure parseEarthquakeData that takes two dates in the format YYYY/MM/DD, accesses the earthquake data from the above USGS URL and returns a list of lists of four numbers representing latitude, longitude, magnitude and depth. The outer list should contain one of these four-number lists for each earthquake between the given dates.

The function will take two dates and access this url and give the data for the earthquakes. Here's what i have so far. I've already written the betweenDates method and it works as it should. It takes three dates and returns true if the first date is between the last two. Here's my parseEarthquake so far.

def parseEarthquakeData(date1, date2):
    dataFile = urllib.request.urlopen("http://neic.usgs.gov/neis/gis/qed.asc")
    latList = []
    longList = []
    magList = []
    depthList = []
    for aline in dataFile:
        aline = aline.decode(ascii)
        splitData = aline.split(',')
        if (betweenDates(splitData[0],date1,date2)):
            latList.append(splitData[2])
            longList.append(splitData[3])
            magList.append(splitData[4])
            depthList.append(splitData[5])
    finalList=[]
    finalList.append(latList)
    finalList.append(longList)
    finalList.append(magList)
    finalList.append(depthList)
    return finalList

It's giving me the error.

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    parseEarthquakeData("2013/07/05","2013/07/10")
  File "C:\Python33\plotEarthquakes.py", line 47, in parseEarthquakeData
    line = aline.decode(ascii)
TypeError: decode() argument 1 must be str, not builtin_function_or_method

I'm not sure what's going wrong. Any help will be appreciated.

You forgot the quotes:

aline = aline.decode('ascii')

What you're currently doing is passing the built-in function ascii , that makes decode confused, and threw the error you're seeing now.

This should fix it, hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM