python 包含有引号和花括号的字符串的格式化

replace 不起作用;
update_per_seconds = "30"
uploadtime_per_seconds = "30"
imei_string = "1234"
### 采用 {0} format 不行
### 下面replace不行 msg = """{"rmtcmd":"set","dev":{"poll":{"timeout":10,"update":update_per_seconds,"delay":15,"uploadtime":uploadtime_per_seconds,"uploadtype":1,"uploadchange":1,"timestamp":1,"timeoutmode":2,"array":0,"y_trigger":1,"prefix":"","cmdcnt":4}},"imei":"imei_string"}""" msg.replace("update_per_seconds",update_per_seconds) msg.replace("uploadtime_per_seconds", uploadtime_per_seconds) msg.replace("imei_string", imei_string) print(msg)
### 下面可行 s1
= """{"rmtcmd":"set","dev":{"poll":{"timeout":10,"update":""" s2 = ""","delay":15,"uploadtime":""" s3 = ""","uploadtype":1,"uploadchange":1,"timestamp":1,"timeoutmode":2,"array":0,"y_trigger":1,"prefix":"","cmdcnt":4}},"imei":\"""" s4 = """\"}""" msg = s1 + update_per_seconds + s2 + uploadtime_per_seconds + s3 + imei_string+ s4 print(msg)

 

posted @ 2024-10-01 19:52  emanlee  阅读(5)  评论(0编辑  收藏  举报