-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