I'm trying to read CSV file from local and
add to Mongodb collection.
Reading a local CSV file:
from flask import jsonify, request
import csv
import json
def csv2json(data):
with open(data, newline='') as csvfile:
reader = csv.DictReader(csvfile)
out = json.dumps([row for row in reader])
return out
I could see this data
Post API: add to csv data to mongoDB collection
# convert csv 2 json and insert MongoDB
@ home_bp.route('/csv2json', methods=['POST'])
def csv_json():
print(CSV_PATH)
csv_data = csv2json(CSV_PATH)
aedata = db.mongo.db.AE
aedata.insert_many(csv_data)
I can see this error: TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping
question from:
https://stackoverflow.com/questions/65661947/how-do-i-insert-json-dump-data-from-csv-to-mongodb-database 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…