AttributeError:“ builtin_function_or_method”对象没有属性“ split” 3.7

时间:2018-12-08 14:21:46

标签: python python-3.x split decode

我需要帮助来修复此代码:

import urllib.request,urllib.parse,urllib.error
    fhand = urllib.request.urlopen("http://data.pr4e.org//romeo.txt")
    counts = dict ()
    for line in fhand:
        lines = line.decode.split()
        for words in lines:
            counts[words] = counts.get(words,0)+1
    print(counts)

运行此代码时出现此错误:

C:\Users\g.p\AppData\Local\Programs\Python\Python37-32>py urllib2.py
Traceback (most recent call last):
  File "urllib2.py", line 5, in <module>
    lines = line.decode.split()
AttributeError: 'builtin_function_or_method' object has no attribute 'split'

1 个答案:

答案 0 :(得分:2)

您应该运行decode函数,否则,它将是内置函数而不是str,因此您不能split该函数

您应该这样写:

lines = line.decode().split()

有关更多信息:Link