首页 > 解决方案 > python文件路径未定义名称错误

问题描述

这是我正在使用的代码,我在文件路径中遇到错误我将文件与其他所有文件放在同一个文件夹中。

# imports
import csv
import json
import dash
import pandas as pd
import sys, getopt, pprint, os
import pymongo
import numpy as np
import dash_leaflet as dl
import plotly.express as px
import plotly.graph_objs as go
import base64

import os
from dash import dcc
from dash import html
from dash import dash_table
from pprint import pprint
from bson.objectid import ObjectId
from bson.json_util import dumps
from pymongo import MongoClient
from dash.dependencies import Input, Output, State
from dbCRUD import BidSystem

##### THIS SECTION ONLY NEEDED FOR INITIAL SETUP #####
# Setup to import the csv file into mongodb through mongodb atlas
#def import_content(filepath):

    # Provide the mongodb atlas url to connect python to mongodb using pymongo and create a connection using MongoClient
client = pymongo.MongoClient("mongodb+srv://myAdminUser:abcd1234@cluster0.zxsdm.mongodb.net/mongodb_eBids?retryWrites=true&w=majority")
    # details for the database and collection names
mng_db = client['mongodb_eBids']
collection_name = 'eBids'
db_cm = mng_db[collection_name]
    #setup to pull the data from csv file and insert into mongodb as json data
cdir = (os.path.dirname(__file__))
file_res = (os.path.join(cdir, filepath))
data = pd.read_csv(file_res)
data_json = json.loads(data.to_json(orient='records'))
db_cm.remove()
db_cm.insert(data_json)

if __name__ == "__main__":
    filepath = 'eBids.csv'
    import_content(filepath)
###### THIS SECTION ONLY NEEDED FOR INITIAL SETUP #####

标签: pythonnameerror

解决方案


看起来你评论了函数定义。取消注释此行再试一次 #def import_content(filepath):


推荐阅读