我有一個存盤在 json 檔案中的資料集,我需要將其上傳到 MongoDB 服務器。如果我使用 Jupyter 筆記本上傳資料,一切正常,但如果我使用 python 腳本代替。代碼完全相同。你建議如何解決這個問題?
這是代碼:
import pandas as pd
import pymongo
from pymongo import MongoClient
import json
import DNS
# Function to upload the dialogue lines to MongoDB Server
def prepare_dataframe():
dialogue_edited = pd.read_json("5lines1response_random100from880_cleaned.json")
dialogue_edited.reset_index(inplace=True)
data_dict = dialogue_edited.to_dict("records")# Insert collection
# To communicate with the MongoDB Server
cluster = MongoClient()
db = cluster['DebuggingSystem']
collection = db['MCS_dialogue']
collection.insert_many(data_dict)
return collection
if __name__ == '__main__':
collection = prepare_dataframe()
這是 python 腳本和 jupyter notebook 的螢屏截圖。我正在使用 Visual Studio 運行筆記本。

uj5u.com熱心網友回復:
代替
if __name__ == '__main__':
collection = prepare_dataframe()
和
collection = prepare_dataframe()
并嘗試運行您的腳本。這里__main__解釋得很好。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/435828.html
下一篇:Mongodb:可用性與一致性
