从网站上阅读

时间:2015-03-03 22:49:14

标签: python python-3.x

def avg_temp_march(f):
'''(file) -> float
Return the average temperature for the month of March
over all years in f.
'''

# We are providing the code for this function
# to illustrate one important difference between reading from a URL
# and reading from a regular file. When reading from a URL,
# we must first convert the line to a string.
# the str(line, 'ascii') conversion is not used on regular files.

march_temps = []

# read each line of the file and store the values
# as floats in a list
for line in f:
    line = str(line, 'ascii') # now line is a string
    temps = line.split()
    # there are some blank lines at the end of the temperature data
    # If we try to access temps[2] on a blank line,
    # we would get an error because the list would have no elements.
    # So, check that it is not empty.
    if temps != []:
        march_temps.append(float(temps[2]))

# calculate the average and return it
return sum(march_temps) / len(march_temps)

所以我有这个功能给了我。你必须输入一个URL(我使用的是http://robjhyndman.com/tsdldata/data/cryer2.dat),它会读取并转换成字符串,但问题是它给了我这个错误

Traceback (most recent call last):
  File "<pyshell#38>", line 1, in <module>
    avg_temp_march(file)
  File "C:\Users\USER\Desktop\work\files.py", line 42, in avg_temp_march
    line = str(line, 'ascii') # now line is a string
TypeError: decoding str is not supported

我的问题是,为什么我收到此错误以及如何解决?

1 个答案:

答案 0 :(得分:0)

我不确定你在追求什么,但这似乎可以解决问题。我不确定的主要问题是avg_temp_march的输入是什么。它是文件,字符串列表,网址还是别的什么?

重要的是你需要知道在调用之前完成了多少处理。

from urllib.request import urlopen

def avg_temp_march(f):
    march_temps = []

    # line does a lot it 
    # strips away new line and whitespace
    # decodes to utf-8
    # splits the values by space
    # does not include lines which are empty
    temps = [line.strip().decode(encoding='utf-8').split() for line in f if line != b'\n']

    # skip first 3 lines as they are not part of the actual data
    for t in temps[3:]:
        # take average temperatures for only march
        # the 3rd month at index 2
        march_temps.append(float(t[2]))

    return sum(march_temps) / len(march_temps)


f = urlopen("http://robjhyndman.com/tsdldata/data/cryer2.dat")
print (avg_temp_march(f))

让我知道这是对还是错,我可以根据需要修改或删除。