我认为其他SO线程会回答我的问题(http://stackoverflow.com/questions/4883751/trouble-reading-json-object-in-python),因为它与我的问题非常相似,但是数据与我案例中的数据略有不同。
我从推特用户数据的Twitter API中提取了大约470条记录,例如:
{
steve: {
follow_request_sent: false,
profile_use_background_image: true,
default_profile_image: false,
geo_enabled: true,
verified: false,
profile_image_url_https: "https://si0.twimg.com/profile_images/1416115378/profile_normal.jpg",
profile_sidebar_fill_color: "F8E846",
id: 1376271,
profile_text_color: "000000",
followers_count: 2042,
profile_sidebar_border_color: "FFFFFF",
location: "Dallas and 51°33′28″N 0°6′10″W",
profile_background_color: "7d0000",
listed_count: 110,
status: {
favorited: false,
contributors: null,
truncated: false,
text: "So Microsoft's cloud is down. Can't say I have noticed. To the cloud! (the Amazon one of course)",
created_at: "Wed Feb 29 15:51:44 +0000 2012",
retweeted: false,
in_reply_to_status_id: null,
coordinates: null,
id: 174884564718723070,
source: "<a href="http://www.tweetdeck.com" rel="nofollow">TweetDeck</a>",
in_reply_to_status_id_str: null,
in_reply_to_screen_name: null,
id_str: "174884564718723073",
place: null,
retweet_count: 0,
geo: null,
in_reply_to_user_id_str: null,
in_reply_to_user_id: null
},
utc_offset: -21600,
statuses_count: 11504,
description: "Network engineer. Cisco, Juniper, F5, HP, EMC, etc. If it is in the data center I deal with it. Arsenal and Mavericks supporter to the max over at @steverossen",
friends_count: 822,
profile_link_color: "0000ff",
profile_image_url: "http://a0.twimg.com/profile_images/1416115378/profile_normal.jpg",
is_translator: false,
show_all_inline_media: false,
profile_background_image_url_https: "https://si0.twimg.com/profile_background_images/192104695/stadium.jpg",
id_str: "1376271",
profile_background_image_url: "http://a2.twimg.com/profile_background_images/192104695/stadium.jpg",
screen_name: "steve",
lang: "en",
profile_background_tile: false,
favourites_count: 0,
name: "Steve Rossen",
notifications: false,
url: "http://steverossen.com",
created_at: "Sat Mar 17 21:36:32 +0000 2007",
contributors_enabled: false,
time_zone: "Central Time (US & Canada)",
protected: false,
default_profile: false,
following: false
},
}
问题是每条记录都以人的推特句柄开头,因此每条记录都有所不同。所以我只能使用:
import json
import csv
f = open('my.json')
data = json.load(f)
f.close()
for item in data:
print item
打印出这些句柄,但无法弄清楚如何在没有钥匙的情况下进入每个人的记录。
我在这里忽略了什么?我至少想得到“描述”,它作为一个键嵌套在用户名中。
答案 0 :(得分:3)
也许我错过了你到底想要什么,但你不能这样做:
import json
f = open('my.json')
data = json.load(f)
f.close()
for key in data.keys():
print data[key]["description"]