通过Python将Json数据导入MongoDB
怪我咯
怪我咯 2017-04-18 10:29:42
0
3
541

首先数据是以标准的json格式的文本。然后想要通过python脚本来导入Mongodb中。
json

{
    "service": "http", 
    "datetime": "2017-03-28 17:23:19", 
    "starttime": "1490692810", 
    "endtime": "1490692999", 
    "port": 80
}{  
    "service": "ewall", 
    "datetime": "2017-03-28 17:23:19",
    "starttime": "1490692810", 
    "endtime": "1490692999", 
    "port": 1328
}

python部分代码:

with open(filen, 'r') as f:
        while 1:
            try:
                jsonstr = f.readline().strip()
                # print jsonstr 可以输出整个json的内容
                if not jsonstr:
                    break
                try:
                    j = json.loads(jsonstr) #这里好像不处理的问题
                    
                except:
                    continue
                jsonlist.append(j)
            except:
                break

请问这个情况要怎么解决呢?谢谢

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(3)
阿神

Your problem is because yours is not in the standard json format. The standard format should be like this

[{
    "service": "http", 
    "datetime": "2017-03-28 17:23:19", 
    "starttime": "1490692810", 
    "endtime": "1490692999", 
    "port": 80
},
{  
    "service": "ewall", 
    "datetime": "2017-03-28 17:23:19",
    "starttime": "1490692810", 
    "endtime": "1490692999", 
    "port": 1328
}]

Second, your data is read in rows. Please tell me what a row of your data looks like

黄舟

@sheep3’s answer is correct.

If you put JSON directly into MongoDB, you can use mongoimport (https://docs.mongodb.com/manu...

If you still want to process data, you can use code like this:

import json
filename = 'test.json'

with open(filename, 'r') as f:
    content = json.load(f)

If the content of the JSON file is larger than the memory, you should open the JSON file through streaming. You can use the ijson package (https://pypi.python.org/pypi/...). Usage is also relatively simple:

import ijson

with open('test.json') as fp:
    objects = ijson.items(fp, "item")
    for object in objects:
        print(object)
迷茫

@Christoph’s answer directly named a simpler and optimized solution, and I learned a trick

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template