-1
我想读取一个JSON文件并将它的每个文档插入到Python中的mongodb中。读取文件并在Ubuntu中插入mongo中的Json对象
代码是:
#!flask/bin/python
import json;
import csv;
import bson;
from pymongo import MongoClient
#connect to database
c = MongoClient('localhost',27017)
db = c.wsn
nodeInfo = db.nodeInfo;
table = db.table;
limit = 0;
skip = 0;
results = [];
#To be able to read csv formated files, we will first have to import the
#csv module.
with open('data.csv', 'rb') as f:
reader = csv.reader(f)
obj = [];
for row in reader:
for field in row:
print(field);
nodeInfo.insert_one(field);
File data is like :
{"name" : "A"}
{"name" : "B"}
{"name" : "C"}
我收到此错误:
TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping
如果我直接使用:nodeInfo.insert_one({“名”:“A”}),它工作正常,但在循环field = {“name”:“x”}。并且它不在循环中工作。 – aditya