Parsing json with python...

Asked 2 years ago, Updated 2 years ago, 43 views

There's a lot of data like this.

20110312000116101|{"place":{"country_code":"US","url":"http:\/\/api.twitter.com\/1\/geo\/id\/5c62ffb0f0f3479d.json","bounding_box":{"type":"Polygon","coordinates":[[[-112.323663,33.29026],[-111.926242,33.29026],[-111.926242,33.892637],[-112.323663,33.892637]]]},"name":"Phoenix","place_type":"city","country":"United States","attributes":{},"id":"5c62ffb0f0f3479d","full_name":"Phoenix, AZ"},"user":{"follow_request_sent":null,"show_all_inline_media":false,"geo_enabled":true,"profile_link_color":"999999","url":"http:\/\/soundcloud.com\/reallilkris","following":null,"verified":false,"profile_sidebar_border_color":"000000","is_translator":false,"listed_count":2,"statuses_count":6200,"profile_use_background_image":true,"profile_background_color":"a30fa3","description":"\u2714Verihighed Account Lil Kris.Self Made G.Check out my music. ","contributors_enabled":false,"profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/215449854\/IMG_0551_opt.JPG","created_at":"Mon Jun 28 07:17:06 +0000 2010","friends_count":270,"protected":false,"profile_image_url":"http:\/\/a1.twimg.com\/profile_images\/1267186210\/xnboor_normal.jpg","time_zone":"Pacific Time (US & Canada)","favourites_count":3,"profile_text_color":"ff0d0d","location":"Smokin Loud in Phx, AZ","name":"Lil Kris","notifications":null,"profile_sidebar_fill_color":"000000","screen_name":"RealLilKris","id":160466271,"id_str":"160466271","lang":"en","profile_background_tile":true,"utc_offset":-28800,"followers_count":268},"coordinates":{"type":"Point","coordinates":[-112.07301182,33.3952667]},"text":"Smokin hella trees tonight.","in_reply_to_status_id":null,"truncated":false,"source":"\u003Ca href=\"http:\/\/twitter.com\/\" rel=\"nofollow\"\u003ETwitter for iPhone\u003C\/a\u003E","favorited":false,"in_reply_to_screen_name":null,"in_reply_to_user_id":null,"created_at":"Sat Mar 12 05:01:15 +0000 2011","in_reply_to_status_id_str":null,"geo":{"type":"Point","coordinates":[33.3952667,-112.07301182]},"contributors":null,"retweeted":false,"id":46435564391641088,"in_reply_to_user_id_str":null,"id_str":"46435564391641088","entities":{"urls":[],"user_mentions":[],"hashtags":[]},"retweet_count":0}

I'm trying to parse my teeth. It's not working. Below is my code.

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

I keep getting empty data. What's the reason?

json python3

2022-09-22 12:44

2 Answers

<a href="http://twitter.com/" rel="nofollow">Twitter for iPhone</a>

Therefore, a decoding error occurs when testing in the shell.

Please save it as a file and try it as below.

import json
with open('.savedJSONFile.json, encoding='UTF8') as f:
    jsonObj = json.loads(f.read())
    print(jsonObj['source'])


2022-09-22 12:44

I think json.decoder.JSONDecodeError occurs because the data you uploaded does not fit the JSON format.

I don't think the append will occur because the exception is caught in the try-exception statement. I tried json parse the string that removed 20110312000116101| from the data you uploaded, and JSONDecodeError occurred.


2022-09-22 12:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.