gpt4 book ai didi

python - 如何处理Python中的KeyError?

转载 作者:太空宇宙 更新时间:2023-11-03 16:27:05 26 4
gpt4 key购买 nike

我正在尝试用 Python 进行一些数据分析,从而尝试使用一些 Twitter 数据来查找不同国家/地区的推文数量。这是我正在使用的代码:

import json
import pandas as pd
import matplotlib.pyplot as plt

tweets_data=[]
with open('/home/surya/tweet.txt','r') as f:
for line in f:
try:
tweet= json.loads(line)
tweets_data.append(tweet)
except:
continue

tweet_table= pd.DataFrame()
tweet_table['country']= map(lambda tweet: tweet["place"]["country"] if tweet["place"] != None else None, tweets_data)

tweets_by_country = tweet_table['country'].value_counts()

fig, ax = plt.subplots()
ax.tick_params(axis='x', labelsize=15)
ax.tick_params(axis='y', labelsize=10)
ax.set_xlabel('Countries', fontsize=15)
ax.set_ylabel('Number of tweets' , fontsize=15)
ax.set_title('Top 5 countries', fontsize=15, fontweight='bold')
tweets_by_country[:5].plot(ax=ax, kind='bar', color='blue')

这会产生错误

KeyError "place"

所以我将代码修改成这样:

import json
import pandas as pd
import matplotlib.pyplot as plt
tweets_data=[]

def keyCheck(key,arr,default):
if key in arr.keys():
return arr[key]
else:
return default

with open('/home/surya/tweet.txt','r') as f:
for line in f:
try:
tweet= json.loads(line)
tweets_data.append(tweet)
except:
continue

tweet_table= pd.DataFrame()
tweet_table['country']= map(lambda tweet: tweet["place"]["country"] if keyCheck("place",tweet,"#default") != None else None, tweets_data)

tweets_by_country = tweet_table['country'].value_counts()

fig, ax = plt.subplots()
ax.tick_params(axis='x', labelsize=15)
ax.tick_params(axis='y', labelsize=10)
ax.set_xlabel('Countries', fontsize=15)
ax.set_ylabel('Number of tweets' , fontsize=15)
ax.set_title('Top 5 countries', fontsize=15, fontweight='bold')
tweets_by_country[:5].plot(ax=ax, kind='bar', color='blue')

但这导致了错误

AttributeError: list object has no attribute "keys"

我的数据格式是:

{"created_at":"Thu Jun 16 13:15:13 +0000 2016","id":743431739238932480,"id_str":"743431739238932480","text":"I fucking hate Ramsey #ENGWAL #EURO2016 https:\/\/t.co\/wkFqOu8iwf","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":343618050,"id_str":"343618050","name":"SamuEars","screen_name":"S88Griff","location":"Derbados","url":null,"description":"27 years old, @RocesterFC1876 footballer, genuine, chilled out, opinionated, but most of all, wind up merchant","protected":false,"verified":false,"followers_count":496,"friends_count":272,"listed_count":1,"favourites_count":1915,"statuses_count":5505,"created_at":"Wed Jul 27 20:53:02 +0000 2011","utc_offset":null,"time_zone":null,"geo_enabled":true,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/636136111191031809\/aQyj3bgK_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/636136111191031809\/aQyj3bgK_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/343618050\/1409857726","default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":{"id":"232163114ebb8671","url":"https:\/\/api.twitter.com\/1.1\/geo\/id\/232163114ebb8671.json","place_type":"city","name":"Etwall","full_name":"Etwall, England","country_code":"GB","country":"United Kingdom","bounding_box":{"type":"Polygon","coordinates":[[[-1.608732,52.874969],[-1.608732,52.887677],[-1.594409,52.887677],[-1.594409,52.874969]]]},"attributes":{}},"contributors":null,"is_quote_status":false,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"ENGWAL","indices":[22,29]},{"text":"EURO2016","indices":[30,39]}],"urls":[],"user_mentions":[],"symbols":[],"media":[{"id":743431733853433856,"id_str":"743431733853433856","indices":[40,63],"media_url":"http:\/\/pbs.twimg.com\/media\/ClEzORsWMAAlcQ3.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/ClEzORsWMAAlcQ3.jpg","url":"https:\/\/t.co\/wkFqOu8iwf","display_url":"pic.twitter.com\/wkFqOu8iwf","expanded_url":"http:\/\/twitter.com\/S88Griff\/status\/743431739238932480\/photo\/1","type":"photo","sizes":{"small":{"w":680,"h":517,"resize":"fit"},"medium":{"w":1178,"h":896,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"large":{"w":1178,"h":896,"resize":"fit"}}}]},"extended_entities":{"media":[{"id":743431733853433856,"id_str":"743431733853433856","indices":[40,63],"media_url":"http:\/\/pbs.twimg.com\/media\/ClEzORsWMAAlcQ3.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/ClEzORsWMAAlcQ3.jpg","url":"https:\/\/t.co\/wkFqOu8iwf","display_url":"pic.twitter.com\/wkFqOu8iwf","expanded_url":"http:\/\/twitter.com\/S88Griff\/status\/743431739238932480\/photo\/1","type":"photo","sizes":{"small":{"w":680,"h":517,"resize":"fit"},"medium":{"w":1178,"h":896,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"large":{"w":1178,"h":896,"resize":"fit"}}}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en","timestamp_ms":"1466082913585"}

问题似乎是 "place" 键在我的 tweet.txt 文件中的某处不可用。

有人可以提出解决方案吗?或者请指出我到底错在哪里?

编辑

我刚刚将代码更新为这个

import json
import pandas as pd
import matplotlib.pyplot as mp

tweets_data=[]
with open('/home/surya/tweet.txt','r') as f:
for line in f:
try:
tweet= json.loads(line)
tweets_data.append(tweet)
except:
continue

tweet_table= pd.DataFrame()
tweet_table['country'] = map(lambda tweet: tweet['place']['country'] if 'place' in tweet is not None and 'country' in tweet['place'] is not None else None, tweets_data)

tweets_by_country = tweet_table['country'].value_counts()

fig, ax = plt.subplots()
ax.tick_params(axis='x', labelsize=15)
ax.tick_params(axis='y', labelsize=10)
ax.set_xlabel('Countries', fontsize=15)
ax.set_ylabel('Number of tweets' , fontsize=15)
ax.set_title('Top 5 countries', fontsize=15, fontweight='bold')
tweets_by_country[:5].plot(ax=ax, kind='bar', color='blue')

现在我收到此错误

TypeError: argument of type 'NoneType' is not iterable

更新

刚刚找到解决方案。仅当推文中的“place”不为 None 时才附加 tweets_data。因此

import json
import pandas as pd
import matplotlib.pyplot as plt

tweets_data=[]
with open('/home/surya/tweet.txt','r') as f:
for line in f:
try:
tweet= json.loads(line)
if 'place' in tweet is not None:
tweets_data.append(tweet)
except:
continue

tweet_table= pd.DataFrame()
tweet_table['country'] = [tweet['place']['country'] for tweet in tweets_data if tweet['place']]

tweets_by_country = tweet_table['country'].value_counts()

fig, ax = plt.subplots()
ax.tick_params(axis='x', labelsize=15)
ax.tick_params(axis='y', labelsize=10)
ax.set_xlabel('Countries', fontsize=15)
ax.set_ylabel('Number of tweets' , fontsize=15)
ax.set_title('Top 5 countries', fontsize=15, fontweight='bold')
tweets_by_country[:5].plot(ax=ax, kind='bar', color='blue')

plt.show()

最佳答案

import json
import pandas as pd
import matplotlib.pyplot as plt


with open("testfile.txt", "r") as f:
tweet_data = [json.loads(line) for line in f]

tweet_table= pd.DataFrame()

tweet_table['country'] = [tweet['place']['country'] for tweet in tweet_data if tweet['place']]

tweets_by_country = tweet_table['country'].value_counts()

fig, ax = plt.subplots()
ax.tick_params(axis='x', labelsize=15)
ax.tick_params(axis='y', labelsize=10)
ax.set_xlabel('Countries', fontsize=15)
ax.set_ylabel('Number of tweets' , fontsize=15)
ax.set_title('Top 5 countries', fontsize=15, fontweight='bold')
tweets_by_country[:5].plot(ax=ax, kind='bar', color='blue')

plt.show()

好吧,我发现了问题,就是推文总是包含一个地点作为 json 中的键,只是如果没有地点,该地点将为空,所以你只需要确保地点不为空之前尝试访问国家/地区。这对我有用,但我只得到英国,因为数据只有 1 条涉及国家/地区的推文

关于python - 如何处理Python中的KeyError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37889335/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com