pymongo应该如何指定数据路径?

2024-04-29 04:32:19 发布

您现在位置:Python中文网/ 问答频道 /正文

users    path     common             config             
                  exchange_rate             
                  prod_data             
                  delivery_fee          

         site     shoppingmall       settings    description
                                                 highlight
                                                 prohibit_words

我做了以下事情,但失败了。在

^{pr2}$

我想对每个顾客都这样做。 我该怎么办? (我喜欢例子,因为我是初学者。。。(T.T.) 谢谢您!!在


Tags: pathconfigdatasettingsrateexchangesiteprod
1条回答
网友
1楼 · 发布于 2024-04-29 04:32:19

我认为失败是因为你的数据不是json。如果要从csv文件插入数据,可以尝试以下操作:

将熊猫作为pd导入

from pymongo import MongoClient

import json



def mongoimport(csv_path, db_name, coll_name, db_url='localhost', db_port=27000)

    """ Imports a csv file at path csv_name to a mongo colection

    returns: count of the documants in the new collection

    """

    client = MongoClient(db_url, db_port)

    db = client[db_name]

    coll = db[coll_name]

    data = pd.read_csv(csv_path)

    payload = json.loads(data.to_json(orient='records'))

    coll.remove()

    coll.insert(payload)

    return coll.count()

此代码易于理解,此代码来自https://gist.github.com/jxub/f722e0856ed461bf711684b0960c8458

相关问题 更多 >