在python中,将一个函数的变量用于另一个函数
我有一个功能:在python中,将一个函数的变量用于另一个函数,python,function,inheritance,Python,Function,Inheritance,我有一个功能: def main(): bla bla bla... output_name = 'Employee' bla bla bla... def tocsv(path): xls_file = pd.ExcelFile(path) names = xls_file.sheet_names for items in names: df = xls_file.parse(items) out_file = output_na
def main():
bla bla bla...
output_name = 'Employee'
bla bla bla...
def tocsv(path):
xls_file = pd.ExcelFile(path)
names = xls_file.sheet_names
for items in names:
df = xls_file.parse(items)
out_file = output_name + '.csv'
df.to_csv(out_file, sep='\t',index=False,header=False, quoting=csv.QUOTE_NONE )
我还有一个功能:
def main():
bla bla bla...
output_name = 'Employee'
bla bla bla...
def tocsv(path):
xls_file = pd.ExcelFile(path)
names = xls_file.sheet_names
for items in names:
df = xls_file.parse(items)
out_file = output_name + '.csv'
df.to_csv(out_file, sep='\t',index=False,header=False, quoting=csv.QUOTE_NONE )
我需要第一个函数main()中定义的变量“output_name”中的文件名。因此,我的输出文件将命名为“Employee.csv”。我怎么做?提前感谢如果变量是在函数中定义的,则它存储在局部范围中。要在多个函数中使用变量,有两种方法 第一:将
output\u name
设置为全局变量:
def main():
global output_name
output_name = 'Employee'
第二:调用tocsv()
时传递参数:
函数中定义的变量是该函数的局部变量。如果要在其他地方使用函数内部定义的变量,应
将该变量返回到调用代码。