如何在f-string python中传递花括号({)作为字符串?[副本]

hts6caw3  于 2023-06-07  发布在  Python
关注(0)|答案(1)|浏览(121)

此问题已在此处有答案

How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?(23答案)
3天前关闭。
由于Hourly Status','{',''),'}','')中的这些括号{}导致查询中的语法错误。如何以f字符串格式将这些括号作为字符串传递?

query = f"""Select fd.serial_number,txidkey,cast(replace(replace(data->>'Hourly Status','{',''),'}','') as text) as description,TO_TIMESTAMP(TIME/1000+19800) as date_time,time,total_min from filter_data fd , total_sum ts
where fd.serial_number = ts.serial_number
and time between {yesterday10PM*1000} and {today6AM*1000}'''
wwtsj6pe

wwtsj6pe1#

使用双花括号{{

query = f"""\
Select fd.serial_number, txidkey, cast(\
replace(replace(data->>'Hourly Status','{{',''),'}}','') as text) \
as description,TO_TIMESTAMP(TIME/1000+19800) as date_time,time,total_min \
from filter_data fd , total_sum ts \
where fd.serial_number = ts.serial_number \
and time between {yesterday10PM*1000} and {today6AM*1000}
'''

你也可以在这里阅读格式化的字符串:
https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals

相关问题