简体   繁体   English

如何在python2.7中的字符串中嵌入一个字符串,其中已经转义了引号?

[英]How to embed a string within a string in python2.7, that has escaped quotes in it?

I am trying with replace two literals with variable for for a payload for an sms:我正在尝试将两个文字替换为用于短信的有效负载的变量:

import requests
url = "https://api.aql.com/v2/sms/send"
token='07ae4cca3f90a49347ccb5cfghypdngthwss85d9ce71862663e4b8162b366ba6c2db8f5f'
msg = "Ambient temperature has dropped to 10 Deg.C"
phone = "441234567890"

payload = "{\"destinations\" : [\"447753448024\"], \"message\": \"Hello, I am a message\"}"
headers = {
    'x-auth-token': token,
    'content-type': "application/json"
}

response = requests.post(url, data=payload, headers=headers)

print(response.text)

I wish to insert the variables phone and msg in place or the literals我希望将变量 phone 和 msg 插入到位或文字

Any help will be gratefully received任何帮助将不胜感激

You may use json attribute from .post() it'll handle the content-type header and the conversion from dict to str您可以使用.post()中的json属性,它将处理content-type header 以及从dictstr的转换

url = "https://api.aql.com/v2/sms/send"
token='07ae4cca3f90a49347ccb5cfghypdngthwss85d9ce71862663e4b8162b366ba6c2db8f5f'
msg = "Ambient temperature has dropped to 10 Deg.C"
phone = "441234567890"

payload = {'destinations':[phone], 'message':msg}
response = requests.post(url, json=payload, headers={'x-auth-token': token})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM