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

Python json.loads显示ValueError:额外数据,python,json,Python,Json,我从一个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"]

我从一个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)
文件“rtfav.py”,第3行,在
data=json.load(infle)
文件“/usr/lib64/python2.7/json/_init__.py”,第278行,已加载
**千瓦)
文件“/usr/lib64/python2.7/json/_init__.py”,第326行,加载
返回\u默认\u解码器。解码
文件“/usr/lib64/python2.7/json/decoder.py”,第369行,在decode中
raise VALUERROR(errmsg(“额外数据”,s,end,len)))
ValueError:额外数据:第88行第2列-第50607行第2列(字符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.com/images/themes/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.com/images/themes/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
} 
{
“贡献者”:空,
“截断”:false,
“文本”:“@HomeShop18#梦想工作到专业椽子”,
“在对状态id的回复中”:null,
“id”:42158440452893696,
“收藏夹计数”:0,
“来源”:“,
“转发”:错误,
“坐标”:空,
“实体”:{
“符号”:[],
“用户_提到”:[
{
“id”:183093247,
“指数”:[
0, 
11
], 
“id_街”:“183093247”,
“屏幕名称”:“家庭旅馆18”,
“姓名”:“家庭旅馆18”
}
], 
“hashtags”:[
{
“指数”:[
12, 
21
], 
“文本”:“梦想工作”
}
], 
“URL”:[]
}, 
“回复屏幕名称”:“家庭旅馆18”,
“id_str”:“421584490452893696”,
“转发计数”:0,
“在用户id的回复中”:183093247,
“偏爱”:错误,
“用户”:{
“跟踪请求发送”:空,
“profile\u use\u background\u image”:真,
“默认_配置文件_图像”:false,
“id”:2254546045,
“已验证”:错误,
“配置文件\u图像\u url\u https”:https://pbs.twimg.com/profile_images/413952088880594944/rcdr59OY_normal.jpeg", 
“配置文件\侧边栏\填充\颜色”:“171106”,
“配置文件\文本\颜色”:“8A7302”,
“粉丝数”:87,
“配置文件\侧边栏\边框\颜色”:“BCB302”,
“id_街”:“2254546045”,
“配置文件\背景\颜色”:“0F0A02”,
“列出的计数”:1,
“配置文件\u背景\u图像\u url\u https”:https://abs.twimg.com/images/themes/theme1/bg.png", 
“utc_偏移量”:空,
“状态计数”:9793,
“描述”:“漂流者。漂流是我的职业。我是未来的技术官僚。”,
“朋友数”:231,
“地点”:“,
“配置文件链接颜色”:“473623”,
“配置文件\图像\ url”:http://pbs.twimg.com/profile_images/413952088880594944/rcdr59OY_normal.jpeg", 
“following”:空,
“已启用地理位置”:false,
“配置文件\u横幅\u url”:https://pbs.twimg.com/profile_banners/2254546045/1388065343", 
“配置文件\背景\图像\ url”:http://abs.twimg.com/images/themes/theme1/bg.png", 
“姓名”:“Jayy”,
“郎”:“恩”,
“配置文件\u背景\u平铺”:false,
“最受欢迎人数”:41,
“屏幕名称”:“JzayyPsingh”,
“通知”:空,
“url”:空,
“创建时间”:“2013年12月20日星期五05:46:00+0000”,
“已启用贡献者”:false,
“时区”:空,
“受保护”:错误,
“默认配置文件”:false,
“is_translator”:错误
}, 
“geo”:空,
“在对用户id的回复中”:“183093247”,
“郎”:“恩”,
“创建时间”:“2014年1月10日星期五10:09:09+0000”,
“过滤级”:“中等”,
“在对状态的回复中”id“str”:空,
“地点”:空
} 

正如您在下面的示例中所看到的,
json.load
(和
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)

您只需从文件中读取,
jsonification
每行代码:

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

这避免了存储中间python对象。只要您在每次
append()
调用中写一条完整的tweet,就应该可以了。

如果您的JSON文件不仅仅是一条JSON记录,也可能会发生这种情况。 JSON记录如下所示:

[{"some data": value, "next key": "another value"}]
它以括号[]打开和关闭,括号内是大括号{}。可以有许多对大括号,但都以一个紧括号结束]。 如果json文件包含以下内容中的多个:

[{"some data": value, "next key": "another value"}]
[{"2nd record data": value, "2nd record key": "another value"}]
然后loads()将失败

我用自己失败的文件验证了这一点

import json

guestFile = open("1_guests.json",'r')
guestData = guestFile.read()
guestFile.close()
gdfJson = json.loads(guestData)
这是因为1_guests.json有一条记录[]。我使用的原始文件all_guests.json有6条记录,以换行符分隔。我删除了5条记录(我已经检查过它是用括号括起来的),并用一个新名称保存了文件。然后loads语句起作用了

错误是

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

另外,我用的是单词record,但那不是官方名称。另外,如果您的文件有像我这样的换行符,您可以循环使用它,一次将一条记录加载到json变量中。

我遇到这个问题是因为我试图加载从MongoDB转储的json文件。这给了我一个错误

JSONDecodeError: Extra data: line 2 column 1
MongoDB JSON转储每行有一个对象,因此对我有用的是:

import json
data = [json.loads(line) for line in open('data.json', 'r')]

如果您想在两行程序中求解它,可以这样做:

with open('data.json') as f:
    data = [json.loads(line) for line in f]

为您的问题提供一个解决方案:

data = [json.loads(line) for line in open('tweets.json', 'r')]

我认为@falsetru提出的在列表中保存dicts并不是一个理想的解决方案

更好的方法是,迭代dict并通过添加新行将其保存到.json

我们的两本字典是

d1 = {'a':1}

d2 = {'b':2}
您可以将它们写入.json

import json
with open('sample.json','a') as sample:
    for dict in [d1,d2]:
        sample.write('{}\n'.format(json.dumps(dict)))
您可以读取json文件,而不会出现任何问题

with open('sample.json','r') as sample:
    for line in sample:
        line = json.loads(line.strip())

简单而有效

嗯,它可能会帮助某些人。当我的json文件是这样时,我得到了相同的错误

{"id":"1101010","city_id":"1101","name":"TEUPAH SELATAN"}
{"id":"1101020","city_id":"1101","name":"SIMEULUE TIMUR"}
我发现它的格式不正确,所以我把它改成了某种

{
  "datas":[
    {"id":"1101010","city_id":"1101","name":"TEUPAH SELATAN"},
    {"id":"1101020","city_id":"1101","name":"SIMEULUE TIMUR"}
  ]
}

我的json文件的格式与问题中的格式完全相同,但没有
{
  "datas":[
    {"id":"1101010","city_id":"1101","name":"TEUPAH SELATAN"},
    {"id":"1101020","city_id":"1101","name":"SIMEULUE TIMUR"}
  ]
}