Python的json.loads显示ValueError:额外的数据

我从一个JSON文件“new.json”获取一些数据,我想筛选一些数据并将其存储到一个新的JSON文件中。 这是我的代码:

import json with open('new.json') as infile: data = json.load(infile) for item in data: iden = item.get["id"] a = item.get["a"] b = item.get["b"] c = item.get["c"] if c == 'XYZ' or "XYZ" in data["text"]: filename = 'abc.json' try: outfile = open(filename,'ab') except: outfile = open(filename,'wb') obj_json={} obj_json["ID"] = iden obj_json["VAL_A"] = a obj_json["VAL_B"] = b 

我得到一个错误,回溯是:

  File "rtfav.py", line 3, in <module> data = json.load(infile) File "/usr/lib64/python2.7/json/__init__.py", line 278, in load **kw) File "/usr/lib64/python2.7/json/__init__.py", line 326, in loads return _default_decoder.decode(s) File "/usr/lib64/python2.7/json/decoder.py", line 369, in decode raise ValueError(errmsg("Extra data", s, end, len(s))) ValueError: Extra data: line 88 column 2 - line 50607 column 2 (char 3077 - 1868399) 

有人能帮我吗?

以下是new.json中的数据示例,文件中有大约1500多个这样的字典

 { "contributors": null, "truncated": false, "text": "@HomeShop18 #DreamJob to professional rafter", "in_reply_to_status_id": null, "id": 421584490452893696, "favorite_count": 0, "source": "<a href=\"https://mobile.twitter.com\" rel=\"nofollow\">Mobile Web (M2)</a>", "retweeted": false, "coordinates": null, "entities": { "symbols": [], "user_mentions": [ { "id": 183093247, "indices": [ 0, 11 ], "id_str": "183093247", "screen_name": "HomeShop18", "name": "HomeShop18" } ], "hashtags": [ { "indices": [ 12, 21 ], "text": "DreamJob" } ], "urls": [] }, "in_reply_to_screen_name": "HomeShop18", "id_str": "421584490452893696", "retweet_count": 0, "in_reply_to_user_id": 183093247, "favorited": false, "user": { "follow_request_sent": null, "profile_use_background_image": true, "default_profile_image": false, "id": 2254546045, "verified": false, "profile_image_url_https": "https://pbs.twimg.com/profile_images/413952088880594944/rcdr59OY_normal.jpeg", "profile_sidebar_fill_color": "171106", "profile_text_color": "8A7302", "followers_count": 87, "profile_sidebar_border_color": "BCB302", "id_str": "2254546045", "profile_background_color": "0F0A02", "listed_count": 1, "profile_background_image_url_https": "https://abs.twimg.comhttp://img.dovov.comthemes/theme1/bg.png", "utc_offset": null, "statuses_count": 9793, "description": "Rafter. Rafting is what I do. Me aur mera Tablet. Technocrat of Future", "friends_count": 231, "location": "", "profile_link_color": "473623", "profile_image_url": "http://pbs.twimg.com/profile_images/413952088880594944/rcdr59OY_normal.jpeg", "following": null, "geo_enabled": false, "profile_banner_url": "https://pbs.twimg.com/profile_banners/2254546045/1388065343", "profile_background_image_url": "http://abs.twimg.comhttp://img.dovov.comthemes/theme1/bg.png", "name": "Jayy", "lang": "en", "profile_background_tile": false, "favourites_count": 41, "screen_name": "JzayyPsingh", "notifications": null, "url": null, "created_at": "Fri Dec 20 05:46:00 +0000 2013", "contributors_enabled": false, "time_zone": null, "protected": false, "default_profile": false, "is_translator": false }, "geo": null, "in_reply_to_user_id_str": "183093247", "lang": "en", "created_at": "Fri Jan 10 10:09:09 +0000 2014", "filter_level": "medium", "in_reply_to_status_id_str": null, "place": null } 

正如你在下面的例子中看到的, json.loads (和json.load )不会解码多个json对象。

 >>> json.loads('{}') {} >>> json.loads('{}{}') # == json.loads(json.dumps({}) + json.dumps({})) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python27\lib\json\__init__.py", line 338, in loads return _default_decoder.decode(s) File "C:\Python27\lib\json\decoder.py", line 368, in decode raise ValueError(errmsg("Extra data", s, end, len(s))) ValueError: Extra data: line 1 column 3 - line 1 column 5 (char 2 - 4) 

如果要转储多个字典,请将它们包装在列表中,转储列表(而不是多次转储字典)

 >>> dict1 = {} >>> dict2 = {} >>> json.dumps([dict1, dict2]) '[{}, {}]' >>> json.loads(json.dumps([dict1, dict2])) [{}, {}] 

我可以build议您不必将所有推文打包到列表中,然后执行json.dumps 。 您可以随时写入文件,然后使用以下命令加载它们:

 tweets = [] for line in open('test.txt', 'r'): tweets.append(json.loads(line)) 

这样你就不必存储中间的python对象。 只要你写一个完整的鸣叫每写入电话这应该工作。

如果您的JSON文件不只是1个JSONlogging,也可能发生这种情况。 一个JSONlogging如下所示:

 [{"some data": value, "next key": "another value"}] 

它用括号[]打开和closures,在括号内是大括号{}。 可以有多对括号,但是它们都以一个闭括号结束]。 如果你的json文件包含多个这样的文件:

 [{"some data": value, "next key": "another value"}] [{"2nd record data": value, "2nd record key": "another value"}] 

那么加载()将失败。

我用我自己的失败文件validation了这一点。

 import json guestFile = open("1_guests.json",'r') guestData = guestFile.read() guestFile.close() gdfJson = json.loads(guestData) 

这是有效的,因为1_guests.json有一个logging[]。 我使用的原始文件all_guests.json有6个logging由换行符分隔。 我删除了5个logging,(我已经检查过被括号保存)并以一个新的名字保存了这个文件。 然后加载声明工作。

错误是

  raise ValueError(errmsg("Extra data", s, end, len(s))) ValueError: Extra data: line 2 column 1 - line 10 column 1 (char 261900 - 6964758) 

PS。 我用logging这个词,但这不是正式的名字。 另外,如果你的文件有像我这样的换行符,你可以通过循环来一次加载一个logging到一个jsonvariables中。