简体   繁体   中英

Data loading on MongoDB server works from jupyter notebook but not from script

I have a dataset stored in a json file and I need to upload it to a MongoDB server. Everything works fine if I upload the data using a Jupyter notebook, but not if I use a python script instead. The code is exactly the same. How do you suggest fixing this?

Here is the code:

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()

Here is a screenshot of the python script and of the jupyter notebook. I'm running the notebook using Visual Studio.

在此处输入图像描述 在此处输入图像描述

Replace

if __name__ == '__main__':
    collection = prepare_dataframe()

with

collection = prepare_dataframe()

and try runnning your script. __main__ explained here pretty well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM