ValueError:使用python导入json文件时出现额外数据错误 [英] ValueError: Extra Data error when importing json file using python
本文介绍了ValueError:使用python导入json文件时出现额外数据错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试构建一个将json文件导入到MongoDB中的python脚本.对于较大的json文件,脚本的这一部分将继续跳转到except ValueError
.我认为这与逐行解析json文件有关,因为很小的json文件似乎可以正常工作.
I'm trying to build a python script that imports json files into a MongoDB. This part of my script keeps jumping to the except ValueError
for larger json files. I think it has something to do with parsing the json file line by line because very small json files seem to work.
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
我收到以下错误消息:
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)
推荐答案
弄清楚了.似乎将其分解成几行是错误的.这是最终代码的样子.
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
这篇关于ValueError:使用python导入json文件时出现额外数据错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文