首先数据是以标准的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
请问这个情况要怎么解决呢?谢谢
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
@Christoph 的回答直接点名了更简单及优化的处理方案,学了一招