[英]double quotes and whitespace as argument to python script in PowerShell
我正在嘗試使用 PowerShell 運行 python 腳本。 在該 python 腳本中,我嘗試使用命令行參數,其中包括雙引號和空格,但無法正常工作。 我懷疑這是 PowerShell 的問題。
(簡化的)python 腳本如下所示:
import sys
print(sys.argv[1])
以下調用和輸出顯示問題
python script.py --arg=foo
--arg=foo
python script.py --arg='\"foo\"'
--arg="foo"
python script.py --arg='\"fo o\"'
--arg="fo
但我最終需要的是
--arg="fo o"
它會去除空格后的所有內容。 我在它工作的 Linux bash 中測試了相同的腳本(在 foo 周圍用雙引號引起來)。 這似乎不是 python 問題而是 PowerShell 問題,任何人都可以幫助我嗎? 最后我想要一個完整的 JSON 字符串作為參數。
9個月沒有回答。 當前的解決方法是使用\"
"
這是錯誤: https ://github.com/PowerShell/PowerShell/issues/1995
使用 '\`'(反斜杠 + 反引號)轉義"
也可以(我不能在代碼風格的降價中包含這里的反引號,因為我不知道如何轉義它......具有諷刺意味)。
請注意,帶有轉義雙引號的整個參數也必須封裝在雙引號中。 將單引號括起來是行不通的。
資料來源: http ://www.rlmueller.net/PowerShellEscape.htm
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.