python - 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 #####
解决方案
看起来你评论了函数定义。取消注释此行再试一次
#def import_content(filepath):
推荐阅读
- python - 如何在使用 python 和 Beautiful soup 抓取的同时访问站点中的兄弟元素
- python - 编写一个快速(数组运算)函数,求解一列中的函数以获得下一个函数值
- javascript - 为什么对 fetch 响应使用解构赋值会导致 .json() 方法消失?
- ionic-framework - 电容器和离子上的 MapKit JS
- visual-studio-2010 - 使用 Visual Studio 2010 重建
- python - 在 Anaconda 环境中安装 AutoSKLearn
- swift - 是否可以从 String 继承自定义类?
- sql - 来自 TSQL 中表的 XML 具有不同的属性值
- php - PHP中的MySQL SELECT查询不返回与直接查询数据库相同的结果
- python - 如何调用 Postgresql 数据库中具有特殊字符的字段名称?