【发布时间】:2021-08-20 18:50:07
【问题描述】:
我想从 read_datasets.py 文件中导入一个数据帧并在 main.py 中处理它。另外使用 mysql 连接的函数。
main.py:
import mysql.connector
import pandas as pd
def con():
connection = None
try:
# declaration of default mysql settings
connection = mysql.connector.connect(
host="xx",
user="xx",
passwd="xx",
db="xx",
)
# If connection is not successful
except:
print("cant connect to database")
return 0
# if connection is successfull
print("connected")
# Making Cursor Object For Query Execution
cursor = connection.cursor()
if __name__ == '__main__':
print("actually in main.py")
#talk to connection mysql
temp1 = pd.read_sql("SELECT xx FROM xx", con() )
# filter all NAN Vlaues in the Dataframe
temp1 = temp1.dropna()
在 read_datasets.py 中:
import pandas as pd
from main import con
temp1 = pd.read_sql("SELECT xx FROM xx", con)
# filter all NAN Vlaues in the Dataframe
temp1 = temp1.dropna()
我得到了错误:
AttributeError: 'NoneType' 对象没有属性 'cursor'
【问题讨论】:
-
cursor = connection.cursor()这是什么? -
一个允许我在 python 中命令数据库的游标,对吧?
-
不应该是
read_datasets.py中的temp1 = pd.read_sql("SELECT xx FROM xx", con())吗? (好像忘了打con,应该是con()而不是con)
标签: python mysql pandas dataframe