這可能是最簡單的問題,我已經嘗試過三重雙引號的解決方案。
但根據 Mixpanel 檔案,他們傳遞了如下所示的單三引號。
我必須在這里傳遞我的日期變數。
fromdate = '2022-03-12'
todate = '2022-03-12'
query1 = '''function main() {
return Events({
from_date: fromdate,
to_date: todate,
event_selectors:[
{'event':'Event name'}
]
})
}'''
print(query1)
3
uj5u.com熱心網友回復:
您可以使用f 字串
fromdate = '2022-03-12'
todate = '2022-03-12'
query1 = f'''function main() {{
return Events({{
from_date: {fromdate},
to_date: {todate},
event_selectors:[
{{'event':'Event name'}}
]
}})
}}'''
請注意,您需要將花括號轉義為{{和}}。
uj5u.com熱心網友回復:
您可以使用以“f”為前綴的字串來將 Python 運算式插入到另一個字串中。
你想要的可能是:
fromdate = '2022-03-12'
todate = '2022-03-12'
query1 = f'''function main() {{
return Events({{
from_date: {fromdate},
to_date: {todate},
event_selectors:[
{{'event':'Event name'}}
]
}})
}}'''
print(query1)
--
Note that the original ocurrences of `{}` have to be doubled so that they are escaped.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/442832.html
標籤:Python python-3.x 混合面板
