[英]How to escape backticks in Python3
我試圖逃避 Python 中的反引號。 例如,我有這個字符串。
>>> s = "my ` string"
>>> s
'my ` string'
執行re.sub("`", "\`", s)
將返回'my \\` string'
。
如何逃避反引號,使其產生my \` string
?
我需要這個,因為我將提供該字符串作為 shell 命令的參數,在 Jupyter Notebook 中運行。
! ./script.sh "$s"
在 Python 中不需要轉義反引號。 您的問題是反斜杠。 這應該很容易解決,只需將第二個參數"\`"
更改為r"\`"
。 這會將其轉換為字符串文字,這樣就不會自動轉義。
這個怎么樣:
s = "my ` string"
for c in s:
if c == '`':
print(r"\`", end='')
else: print(c, end='')
Output: my \` string
您可以使用 urllib。 password = urllib.parse.quote_plus(password)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.