Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

python - How do I insert Json.dump() data from CSV to Mongodb database

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 enter image description here

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...