首页 > 解决方案 > 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

关于如何将输出从一个功能传递到另一个功能的任何建议?

标签: pythonpandas

解决方案


第二个函数缺少参数:

def assignment(df):

推荐阅读