Error using Python function input method

Asked

Viewed 340 times

2

I have a problem with my code. I have 3 functions, one of them uses an input method called tweet but when I call the error function and does not perform, maybe I did not understand this part of input methods but I reviewed a few times and to me seems to be right.

I read a file with this function:

def le_tweets():
    lista = []
    file = codecs.open("tweets_prova.txt", encoding='utf-8')
    for l in file:
        lista.append(l.replace('\n',''))
    return lista

I take the value of the previous function and return it to a variable later

def carregaTweets():
        lista = ''
        lista = le_tweets()
        return lista

And this function to separate the text by words

def separa_palavras(tweet):
    return tweet.split(' ')

I can receive the text from the file I read, but when I use the error separa_words(tweet) and I don’t know what to change to resolve.

tweet = carregaTweets()
lista = separa_palavras(tweet)
print(lista)
  • 1

    What’s the mistake? Be more specific

  • Traceback (Most recent call last): File "C: Users Jhonatan.Teodoro Desktop work activity_raaluno1_raaluno2.py", line 58, in <module> list = (separa_words(tweet)) File "C: Users Jhonatan.Teodoro Desktop work activity_raaluno1_raaluno2.py.", line 23, in separa_palavras Return tweet.split(' ') Attributeerror: 'list' Object has no attribute 'split'

1 answer

0


His method carregaTweets() returns a string list. Therefore, separa_palavras(tweet) receives a list of strings per parameter. You are trying to call split in the whole list, being that you can not give split in a list, only in a string.

  • I get it. I’ll try to do it here, thanks man!

  • Got it man, thanks!!

  • 2

    I’m glad I helped, @Jhonatanteodoro. If the answer solved your problem, please consider marking as accepted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.