首先数据是以标准的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
请问这个情况要怎么解决呢?谢谢
Your problem is because yours is not in the standard json format. The standard format should be like this
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:
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:
@Christoph’s answer directly named a simpler and optimized solution, and I learned a trick