我有一个数据库接口类,该类从熊猫pd.read_sql
函数返回查询。我想将pd.read_sql
参数传递给方法,以便在调用函数时可以使用它,如下所示:
import pyodbc
import pandas as pd
class Database:
def __init__():
driver_path = 'blablabla'
def select(self, query, **kwargs):
con = pyodbc.connect(driver_path)
result = pd.read_sql(query, con, kwargs)
con.close()
return result
但这不起作用。有没有办法像我使用R的省略号那样将参数传递给函数内部的函数? (...
)
例如,我想运行如下查询:
query = "Select * from Table Where Group = '?'"
Database.select(query, params='GROUP1')
答案 0 :(得分:1)
您只需在**
之前添加kwargs
,就像在select
方法中传递它的方式一样:
class Database:
def __init__():
driver_path = 'blablabla'
def select(self, query, **kwargs):
con = pyodbc.connect(driver_path)
result = pd.read_sql(query, con, **kwargs)
con.close()
return result