python - 从另一个文件导入一个 pd.read_sql() DataFrame 以进一步处理它
问题描述
我想从 read_datasets.py 文件中导入一个数据框并在 main.py 中处理它。另外使用 mysql 连接的函数。
主要.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”对象没有属性“光标”
解决方案
我只有使用 SQLAlchemy 执行此操作的经验,所以我将发布如何使用该包执行此操作:
from sqlalchemy import create_engine
def db_connection(self):
engine = create_engine('mysql+pymysql://{0}:{1}@{2}:3306/{3}'.format(username, password, host, db_name), echo=False)
connection = engine.connect()
return connection
^我没有定义密码等变量,但请注意,上面的代码不起作用,因为你需要这样做!
然后您可以致电:
pd.read_sql("SQL query here", db_connection())
推荐阅读
- javascript - Chrome 扩展中的 popup.js 未收到从 contentScript.js 发送的消息
- blazor - 如何在组件中填充渲染片段
- reactjs - 在 Reactjs 应用程序中创建用户时,Firebase 身份验证超时
- python - 从 pdf 文件导出表格
- frame - CSMA/CD 中的最小帧大小
- python - 在python中对包含字符串和数字的txt文件进行排序
- node.js - 用快捷键在VSCode中执行一个Typescript文件
- java - AroundInvoke:好奇的名字?
- android - 如何找出使用我的应用程序的用户年龄的全部统计数据?
- security - 缓冲区溢出示例