python - Python将数组从函数传递到函数
问题描述
找不到过去几年写的任何解决方案。
我希望将变量从一个函数传递到另一个函数,而无需重新运行 SQL 连接。
第一个功能是:
def SQL_Country():
conn = psq.connect("localhost","root","root","world",cursorclass=psq.cursors.DictCursor)
query = "SELECT * FROM country"
with conn:
cursor = conn.cursor()
cursor.execute(query)
country = cursor.fetchall()
global df
df = pd.DataFrame(country, columns=["Code", "Name", "Continent", "Population", "HeadOfState"])
我希望传递输出的第二个函数SQL_Country()
是:
def assignment():
## do something here
elif Choice == 6:
try:
x = df
except NameError:
x = None
if x is None:
print()
df = SQL_Country(df)
我收到以下错误:
File "Python.py", line 185, in assignment
df = SQL_Country(df)
UnboundLocalError: local variable 'df' referenced before assignment
关于如何将输出从一个功能传递到另一个功能的任何建议?
解决方案
第二个函数缺少参数:
def assignment(df):
推荐阅读
- reactjs - 为什么代理在与 webpack 一起使用时不起作用
- java - 从 Firestore 查询数据到 Arraylist 但一无所获?
- ios - 没有使用滑块调用accessibilityIncrement 和accessibilityDecrement
- angular - 如何在 iOS Nativescript 中获取 SSID(网络名称)
- javascript - 没有 JQuery 的 VueJS 中的弹出菜单?
- react-native - 创建 React Native Github 存储库
- ms-access - MS 访问搜索按钮条件导致运行时错误 3075 额外
- jenkins - 我们可以将普通的 results.log 文件转换为 xunit 或 allure xml 格式吗?
- python - ImportError:没有名为 playsound 的模块
- excel - VBA - 在数据间隙之间插入合并行