python - python - 如何将变量放在python中的一个sting字典中?
问题描述
我正在尝试与 Trakt.tv api 交互,但我正在努力在字符串字典中创建一个变量。
api OAuth 调用服务器,提供 2 个访问代码,其中一个代码需要转发到以下服务器调用。像这样:
values = """{
"code": "code provide by the api",
"client_id": "code provide by user",
"client_secret": "code provide by user"
}
"""
headers = {
'Content-Type': 'application/json'
}
request = Request('https://api.trakt.tv/oauth/device/token', data=values, headers=headers)
我在变量 api_code 中有所需的代码,并希望将此变量放在字符串中,如下例所示。
values = """{
"code": f"api_code",
"client_id": f"user_code",
"client_secret": f"user_code_2"
}
"""
headers = {
'Content-Type': 'application/json'
}
request = Request('https://api.trakt.tv/oauth/device/token', data=values, headers=headers)
解决方案
IIUC 你可以.format()
这样使用:
values = """{{
"code": f"{0}",
"client_id": f"user_code",
"client_secret": f"user_code_2"
}}
"""
api_code = 'abc123'
values = values.format(api_code)
请注意,为了获取{}
格式字符串中的实际字符,您需要将它们加倍,就像我在第 1 行和第 4 行中所做的那样
推荐阅读
- php - 允许的内存大小 X 已用尽(试图分配 Y),其中 Y < X
- jquery - Microsoft Edge 上的菜单项消失
- c# - MDI 父母不在客户区保留表格
- python-3.x - 如何使用 Selenium + Python 从网站访问 id 中的子 id
- validation - SuiteCRM:在标准模块中执行自定义验证的推荐方法?
- java - 组合框更改应用程序中的字体
- mfc - 在 CTabCtrl 中,我可以在运行时更改选项卡的图标吗
- c# - net7mma rtsp 流格式
- azure-ad-b2c - B2C 登录 - 具有动态内容的 UI
- angular-directive - 在 Angular 6 指令中注入组件时出错