Python:2.7版:NameError:未定义名称“data”

2024-04-30 03:01:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python主python脚本,它从另一个python文件导入函数,当它到达函数int_brief(data)时会出现以下错误:

Traceback (most recent call last):
      File "test.py", line 28, in <module>
        int_brief(data)
    NameError: name 'data' is not defined

下面的函数在主python文件中被调用,它是从另一个具有这些功能的python文件导入的。平台是Linux和Python2.7,所有运行到包含变量data的行的代码都正常工作:

def int_brief(data):

    commandOutput = commandStatus('show ip int brief',expect_string=r'#')
    shellOut = redirect_shell()
    data = pd.read_fwf(StringIO(commandOutput),  widths=[23, 16, 4, 7, 22, 8])  
    return commandOutput, shellOut, data

我尝试过在函数int_brief()中运行没有参数data的代码,但是仍然抛出了错误


Tags: 文件函数代码脚本mostdata错误call
1条回答
网友
1楼 · 发布于 2024-04-30 03:01:27

似乎函数根本不需要data参数,因为它是在函数中创建的。尝试删除参数,然后调用不带参数的函数:

def int_brief():
    commandOutput = commandStatus('show ip int brief',expect_string=r'#')
    shellOut = redirect_shell()
    data = pd.read_fwf(StringIO(commandOutput),  widths=[23, 16, 4, 7, 22, 8])  
    return commandOutput, shellOut, data

stuff = int_brief()

如果由于某种原因您不能更改int_brief函数,那么您也可以将任何作为data参数传递,因为它在被赋予新值之前从未使用过:

stuff = int_brief("does not matter, never used")

相关问题 更多 >