python - 如何在python中的json数据中传递字符串变量
问题描述
error - {"type":"error","message":"无效的手机号码。您必须输入少于 10 位数字或 API 中手机号码字段中有字母字符","code":"202 "}
import http.client
ph='9999999999'
conn = http.client.HTTPSConnection("api.msg91.com")
print(ph)
payload = '''{"sender": "SOCKET",
"route": "4",
"country": "91",
"sms": [
{
"message": "Testing",
"to": "'+str(ph)+'"
}
]}'''
解决方案
You can either use format
or f-strings
concept of Python.
Below code snippet is an example of f-strings
var_name= "World"
payload = f"Hello {var_name}"
print(payload)
Hello World
For format:
var_name = "World"
payload = "Hello {var}".format(var=var_name)
print(payload)
Hello World
推荐阅读
- python - 如何在 QuickBooks Online 中获取会计“截止日期”
- json - 为什么通过邮递员发布 json 时出现范围不足的错误?
- c++ - 为什么 dlsym 在动态加载的库中找不到函数?
- powershell - 检查远程端口是否打开并使用时间戳记录
- javascript - 从 UserAgent 对象(koa-useragent)获取属性
- javascript - PM2重启延迟Node.js
- if-statement - 如何查看页面上的段落并搜索关键字?(并且还建立一个重定向到正确结果的关键字列表/目录)
- ag-grid-angular - Ag Grid 为什么在运行“onRowHeightChanged()”后单元格渲染器组件会更新
- python - pip install 在 colab 中无法用于 trumania 库
- ruby-on-rails - Rails:基于关联模型属性的 ActiveRecord 查询