I'm currently working on a program to retrieve a list of tweets on a given topic. Until then I manage to retrieve them and save them in a JSON file which is perfect.
The problem comes when I try to "tokenize" this list of tweets.
I'm having the following error:
Traceback (most recent call last):
File "C:\Users\TheoLC\Desktop\python\twitter_search\collect+200tw.py", line 77, in <module>
tweet_token = tweet['text'].tokenize()
TypeError: string indices must be integers
And this is the code :
with open("%s_tweets.json" % search_word, 'a') as f:
for tweet in new_tweets:
json.dump(tweet._json, f, indent=4)
with open("%s_tweets.json" % search_word, 'r+') as f:
for tweet in f:
tweet_token = tweet['text'].tokenize()
print('Tweet tokenize : ' + tweet_token)
I also have a second concern which is:
In my program I translate the search word into several languages in order to get as many tweets as possible from my JSON file.
The problem is that instead of getting a JSON with several tweets from several languages I would like all tweets to be translated into English.
So I try to apply the reverse process as follows:
for tweet in new_tweets_fi:
tweet['text'] = translator.translate(tweet['text'], src='fi', dest='en')
print("Les tweets en finlandais ont été traduis")
for tweet in new_tweets_fr:
tweet['text'] = translator.translate(tweet['text'], src='fr', dest='en')
print("Les tweets en francais ont été traduis")
And here is the error that comes back:
Traceback (most recent call last):
File "C:\Users\TheoLC\Desktop\python\twitter_search\collect+200tw.py", line 52, in <module>
tweet['text'] = translator.translate(tweet['text'], src='fi', dest='en')
TypeError: 'Status' object is not subscriptable
A huge thanks to those who will be able to help me