0

If I have a .txt file that contains multiple dictionaries of twitter user info, and they are not in different lines, such as this:

{"created_at": "Thu Sep 12 07:24:17 +0000 2013", "favorited": false, "id": 378056455997648896, "lang": "it", "retweeted": false, "source": "http://instagram.com\" rel=\"nofollow\">Instagram", "text": "Lucca http://t.co/IsVfi8GePP", "truncated": false}{"created_at": "Fri Apr 25 14:48:45 +0000 2008", "favourites_count": 4, "followers_count": 11, "friends_count": 142, "id": 14527251, "lang": "en", "listed_count": 1, "location": "Carroll Gardens, Brooklyn", "name": "sasha718", "profile_background_color": "C0DEED", "profile_background_tile": false"profile_link_color": "0084B4", "profile_text_color": "333333", "protected": true, "screen_name": "sasha718", "statuses_count": 5, "time_zone": "Eastern Time (US & Canada)", "utc_offset": -14400}

How to convert them to python dictionaries, or even better, back to twitter user objects? I know how to do it when their is only one dictionary, but get JSONDecodeError message when their are multiple dictionaries that are in the same line. How to set up an iterator in this case??

JJ Fish
  • 1
  • 1
  • 1
    load the file with json, that should work. – Riyaz Sep 07 '15 at 16:22
  • json.load() works if there is only one dictionary. But if there are several dictionaries, I get jsonDecodeError File "C:\Python27\lib\site-packages\simplejson\__init__.py", line 459, in load use_decimal=use_decimal, **kw) File "C:\Python27\lib\site-packages\simplejson\__init__.py", line 516, in loads return _default_decoder.decode(s) File "C:\Python27\lib\site-packages\simplejson\decoder.py", line 373, in decode raise JSONDecodeError("Extra data", s, end, len(s)) simplejson.scanner.JSONDecodeError: Extra data: line 1 column 515 - line 1 column 134054 (char 514 - 134053) – JJ Fish Sep 07 '15 at 16:44
  • besides, json.load only convert it back to dictionary, is there a way to convert to a user object? – JJ Fish Sep 07 '15 at 17:00
  • pragmatically: Read into a variable; split the text by replace('}{',r'}\n{') then split(r'\n') and run json.load over the array filling up your jsonlike dictionary. – ABri Sep 09 '15 at 20:45

0 Answers0