[英]Calling a python script from another script and passing variables to it
我有一個python腳本,其中包含一個大型函數,該函數可以過濾列表並將內容導出到excel。 我希望能夠制作不同的python腳本,在其中可以為不同的情況指定不同的過濾器,並通過應用過濾器的函數將它們傳遞給我的python腳本。
例如
#Script called function_script with function to apply filters
def my_funct(filter1, filter2, filter3, etc):
apply filters to list
export results to excel
對於一些常見的用例,我將使用不同的過濾器,而不是更改過濾器,我寧願為每個用例創建一個python腳本,該腳本調用包含我的函數的腳本並通過過濾器。 就像是。
#One script where filters for category 1 are defined
filter1 = 'Foo'
filter2 = 'Bar'
filter3 = 'Test'
function_script(filter1, filter2, filter3)
假設具有大型功能的文件與所有其他文件位於一個目錄中:
some folder
|-functions_file.py
|-main.py
因此,您的功能文件將包含您的功能,例如:
def add(num1, num2):
return num1+num2
def subtract(num1, num2):
return num1-num2
#here is example function to show you how to pass more values to function
def add_all(numbers):
#if you need individual values
for number in numbers:
print number
return sum(number)
並從main.py
調用該函數,您可以執行以下操作:
import functions_file
print(functions_file.add(1,20)) #prints 21 if you're on py2 you don't need () for print
print(functions_file.add_all([1,2,3,4,5,6]))
#you pass parameters in list -> 21 is output
execfile(filename [,globals [,locals]])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.