简体   繁体   English

Python:解码错误(ascii)

[英]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. 编写过程parseEarthquakeData的合同,文档字符串和实现,该过程采用YYYY / MM / DD格式的两个日期,从上述USGS URL访问地震数据,并返回一个包含四个数字的列表,分别代表纬度,经度,震级和深度。 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. 该函数将使用两个日期并访问该URL,并提供地震数据。 Here's what i have so far. 这是我到目前为止所拥有的。 I've already written the betweenDates method and it works as it should. 我已经编写了betweenDates方法,它可以正常工作。 It takes three dates and returns true if the first date is between the last two. 它需要三个日期,如果第一个日期在最后两个日期之间,则返回true。 Here's my parseEarthquake so far. 到目前为止,这是我的parseEarthquake。

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. 您当前正在执行的操作是传递内置函数ascii ,使decode混乱,并抛出您现在看到的错误。

This should fix it, hope this helps! 这应该修复它,希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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