gaurav
gaurav

Reputation: 443

AttributeError: 'builtin_function_or_method' object has no attribute 'split' 3.7

I need help to fix this code:

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)

I am getting this error while running this code:

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'

Upvotes: 1

Views: 1013

Answers (1)

Mehrdad Pedramfar
Mehrdad Pedramfar

Reputation: 11073

you should run decode function, otherwise, it will be the built-in function not str, so you cannot split the function

You should write like this:

lines = line.decode().split()

For more info: Link

Upvotes: 2

Related Questions