Im currently playing a bit with the twitter API. So far i managed to get a specific tweet as json and prettyprint that. Here is the output:
{
"contributors": null,
"coordinates": null,
"created_at": "Sun Jun 28 12:32:35 +0000 2020",
"display_text_range": [
19,
23
],
"entities": {
"hashtags": [],
"symbols": [],
"urls": [],
"user_mentions": [
{
"id": 11883745733410470,
"id_str": "11883745733410470",
"indices": [
0,
10
],
"name": "Account1",
"screen_name": "account1"
},
{
"id": 27822535,
"id_str": "27822535",
"indices": [
11,
18
],
"name": "Account2",
"screen_name": "account2"
}
]
},
"favorite_count": 0,
"favorited": false,
"filter_level": "low",
"geo": null,
How can i store all values of the key entities -> user_mentions -> screen_name in a variable, list, or whatever? I just want to store them and do something later on.
So far i got:
def on_data(self, data):
# Twitter returns data in JSON format - we need to decode it first
decoded = json.loads(data)
#print(json.dumps(decoded, indent=4, sort_keys=True))
tweet_id = decoded['id_str']
username = decoded['user']['screen_name']
text = decoded['text']
is_reply = decoded['in_reply_to_status_id']
mentions = decoded['entities']['user_mentions']['screen_name']
Which gives me an error because it returns more than one screen_name of course.
mentions = decoded['entities']['user_mentions']['screen_name']
TypeError: list indices must be integers or slices, not str
3
Answers
decoded['entities']['user_mentions']
is a list, so you can only access it with indices.The error gives you a good hint.
decoded['entities']['user_mentions']
is a list, so you can get all screen names using:
If you want a long string of all screen names, or do various other thing you can also use list functions like join, as @Sushanth noticed.
decoded['entities']['user_mentions']
is a list.You can use
type()
to see itTo get single name you have to use index
[0]
,[1]
, and later["screen_name"]
To get all of them you need
for
-loopor list comprehension
Minimal working example