I have two text files that I wish to work with using Pandas. The files were created in the exact same way and are very similar, except for some of the content inside. However, my program does not work with one of the text files, but does work with the other. Here is my error:
Traceback (most recent call last):
File "E:\Holiday Project\Politic\store.py", line 19, in <module>
tweets['text'] = list(map(lambda tweet: tweet['text'], tweets_data))
File "E:\Holiday Project\Politic\store.py", line 19, in <lambda>
tweets['text'] = list(map(lambda tweet: tweet['text'], tweets_data))
KeyError: 'text'
and here is my code:
import json
import pandas as pd
from textblob import TextBlob
tweets_data_path = 'filename.txt'
tweets_data = []
tweets_file = open(tweets_data_path, "r")
for line in tweets_file:
try:
tweet = json.loads(line)
tweets_data.append(tweet)
except:
continue
print (len(tweets_data))
tweets = pd.DataFrame()
tweets['text'] = list(map(lambda tweet: tweet['text'], tweets_data))
tweets['lang'] = list(map(lambda tweet: tweet['lang'], tweets_data))
tweets['country'] = list(map(lambda tweet: tweet['place']['country'] if tweet['place'] != None else None, tweets_data))
avg = 0
for lol in tweets['text']:
tweet = TextBlob(text)
avg = tweet.sentiment.polarity + avg
avg = avg/len(tweets)
print(avg)
The Usual Solution: . If the KeyError is raised from a failed dictionary key lookup in your own code, you can use . get() to return either the value found at the specified key or a default value.
A Python KeyError is raised when you try to access an item in a dictionary that does not exist. You can fix this error by modifying your program to select an item from a dictionary that does exist. Or you can handle this error by checking if a key exists first.
Typically this error occurs when you simply misspell a column names or include an accidental space before or after the column name.
tweet['text'] does not seem to exist. A key error is generated when you try to access a key in a hash map/dictionary that does not exist. for example
myDict = {"hello": 1, "there": 2}
print myDict["hello"] #this prints 1
print myDict["friend"] #this will generate a key error because it does not exist
look on these lines:
tweets = pd.DataFrame()
tweets['text'] = list(map(lambda tweet: tweet['text'], tweets_data))
You probably try to extract tweet['text'] which does not exist in some of the dictionaries. If the "text" field exists in only some of the lines you are loading, than you may want to write something like that:
tweets = pd.DataFrame()
tweets['text'] = [tweet.get('text','') for tweet in tweets_data]
tweets['lang'] = [tweet.get('lang','') for tweet in tweets_data]
#and so on...
If for some reason, in some of the jsons "text" do not exists, you will get ' ' in the DataFrame.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With