简体   繁体   English

ValueError:使用python导入json文件时出现额外数据错误

[英]ValueError: Extra Data error when importing json file using python

I'm trying to build a python script that imports json files into a MongoDB. 我正在尝试构建将json文件导入到MongoDB中的python脚本。 This part of my script keeps jumping to the except ValueError for larger json files. 对于较大的json文件,脚本的这一部分将继续跳转到except ValueError I think it has something to do with parsing the json file line by line because very small json files seem to work. 我认为这与逐行解析json文件有关,因为很小的json文件似乎可以工作。

def read(jsonFiles):
from pymongo import MongoClient

client = MongoClient('mongodb://localhost:27017/')
db = client[args.db]

counter = 0
for jsonFile in jsonFiles:
    with open(jsonFile, 'r') as f:
        for line in f:
            # load valid lines (should probably use rstrip)
            if len(line) < 10: continue
            try:
                db[args.collection].insert(json.loads(line))
                counter += 1
            except pymongo.errors.DuplicateKeyError as dke:
                if args.verbose:
                    print "Duplicate Key Error: ", dke
            except ValueError as e:
                if args.verbose:
                    print "Value Error: ", e

                    # friendly log message
            if 0 == counter % 100 and 0 != counter and args.verbose: print "loaded line:", counter
            if counter >= args.max:
                break

I'm getting the following error message: 我收到以下错误消息:

Value Error:  Extra data: line 1 column 10 - line 2 column 1 (char 9 - 20)
Value Error:  Extra data: line 1 column 8 - line 2 column 1 (char 7 - 18)

Look at this example: 看这个例子:

s = """{ "data": { "one":1 } },{ "1": { "two":2 } }"""
json.load( s )

It will produce the "Extra data" error like in your json file: 它将在您的json文件中产生“额外数据”错误:

ValueError: Extra data: line 1 column 24 - line 1 column 45 (char 23 - 44) ValueError:额外数据:第1行第24列-第1行第45列(字符23-44)

This is because this is not a valid JSON object. 这是因为这不是有效的JSON对象。 It contains two independend "dict"s, separated by a colon. 它包含两个独立的“ dict”,以冒号分隔。 Perhaps this could help you finding the error in your JSON file. 也许这可以帮助您在JSON文件中找到错误。

in this post you find more information. 这篇文章中,您可以找到更多信息。

Figured it out. 弄清楚了。 Looks like breaking it up into lines was the mistake. 似乎将其分解成几行是错误的。 Here's what the final code looks like. 这是最终代码的样子。

counter = 0
for jsonFile in jsonFiles:
    with open(jsonFile) as f:
        data = f.read()
        jsondata = json.loads(data)
        try:
            db[args.collection].insert(jsondata)
            counter += 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM