簡體   English   中英

使用 tcl 腳本調用 python function 和 arguments

[英]calling python function with arguments using tcl script

我有一個 python 腳本 (sample.py),它有一個 function 和 3 個 arguments。 其中第一個兩個 arguments 是十六進制,第三個是字符串。

def sampleFun(gen_lane,sw_state,test_name):`
    
    ### code segment 

    output = "some string"
    return output

我必須使用 tcl 腳本來調用 sample.py 中存在的這個 function sampleFun。

怎么做? 我試過這個 tcl 命令:

proc call_python { } {
    set gen_lane 0x10500000
    set sw_state 0x0000000B
    set test_case "linkup"
    set result [exec python -c "import sample; print sample.sampleFun($gen_lane,$sw_state,$test_case)"]
    puts $result

}

但是我收到了未定義名稱“linkup”的錯誤。

那么,如何將字符串參數從 tcl 傳遞給 python function?

您需要為 Python 報價。

對於這樣的幾乎任意值,您可以嘗試以下操作:

set result [exec python -c "import sample; print sample.print_file($gen_lane,$sw_state,r'''$test_case''')"]

這使用了這樣一個事實,即 Python 中'''引用的字符串可以包含換行符和單個'字符,並且r字符串可以包含反斜杠。 剩下的可能導致問題的案例很少見。

您知道您的示例 Python 代碼說sampleFun但您的測試代碼說sample.print_file 我想你可以解決這個問題……

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM