在 python 字典中使用字符串值时如何避免 KeyError
How to avoid KeyError when using a string value in python dict
我正在使用保管箱 api 上传这样的文件:
token = 'This is the token'
file_name = 'This is the file_name'
headers = {
"Authorization": "Bearer {}".format(token),
"Content-Type": "application/octet-stream",
"Dropbox-API-Arg": "{\"path\":\"/my/path/my-file-name\",\"mode\":{\".tag\":\"overwrite\"}}"
}
data = open("my-file-name", "rb").read()
r = requests.post(url, headers=headers, data=data)
问题是文件名会在我 运行 这样的时候改变,所以我需要做类似的事情:
headers = {
"Authorization": "Bearer {}".format(token),
"Content-Type": "application/octet-stream",
"Dropbox-API-Arg": "{\"path\":\"/my/path/{}\",\"mode\":{\".tag\":\"overwrite\"}}".format(file_name)
}
但是当我这样做时,我收到错误消息:
Traceback (most recent call last):
File "headertest.py", line 6, in <module>
"Dropbox-API-Arg": '{"path":"/my/path/{}","mode":{".tag":"overwrite"}}'.format(file_name)
KeyError: '"path"'
使用 .format 时,您需要将字符串中应有的赞誉加倍,例如:{{ }}。基本上 {{ 相当于转义引号的斜杠,如下所示:\"。您的代码应如下所示:
s = '{{\"path\":"/my/path/{}","mode":{{".tag":"overwrite"}}}}'.format(file_name)
您在 json 字符串中嵌套了 {}
。您应该首先创建一个普通的 dict
并将 file_name
放入其中。然后 json.dumps()
将其放入您的 header 字典中。
import json
token = 'abc'
file_name = 'This is the file_name'
arg_dict = {"path":"/my/path/{}".format(file_name),"mode":{"tag":"overwrite"}}
arg_s = json.dumps(arg_dict)
headers = {
"Authorization": "Bearer {}".format(token),
"Content-Type": "application/octet-stream",
"Dropbox-API-Arg": arg_s
}
print(headers)
输出:
{'Authorization': 'Bearer abc', 'Content-Type': 'application/octet-stream', 'Dropbox-API-Arg': '{"path": "/my/path/This is the file_name", "mode": {"tag": "overwrite"}}'}
我正在使用保管箱 api 上传这样的文件:
token = 'This is the token'
file_name = 'This is the file_name'
headers = {
"Authorization": "Bearer {}".format(token),
"Content-Type": "application/octet-stream",
"Dropbox-API-Arg": "{\"path\":\"/my/path/my-file-name\",\"mode\":{\".tag\":\"overwrite\"}}"
}
data = open("my-file-name", "rb").read()
r = requests.post(url, headers=headers, data=data)
问题是文件名会在我 运行 这样的时候改变,所以我需要做类似的事情:
headers = {
"Authorization": "Bearer {}".format(token),
"Content-Type": "application/octet-stream",
"Dropbox-API-Arg": "{\"path\":\"/my/path/{}\",\"mode\":{\".tag\":\"overwrite\"}}".format(file_name)
}
但是当我这样做时,我收到错误消息:
Traceback (most recent call last):
File "headertest.py", line 6, in <module>
"Dropbox-API-Arg": '{"path":"/my/path/{}","mode":{".tag":"overwrite"}}'.format(file_name)
KeyError: '"path"'
使用 .format 时,您需要将字符串中应有的赞誉加倍,例如:{{ }}。基本上 {{ 相当于转义引号的斜杠,如下所示:\"。您的代码应如下所示:
s = '{{\"path\":"/my/path/{}","mode":{{".tag":"overwrite"}}}}'.format(file_name)
您在 json 字符串中嵌套了 {}
。您应该首先创建一个普通的 dict
并将 file_name
放入其中。然后 json.dumps()
将其放入您的 header 字典中。
import json
token = 'abc'
file_name = 'This is the file_name'
arg_dict = {"path":"/my/path/{}".format(file_name),"mode":{"tag":"overwrite"}}
arg_s = json.dumps(arg_dict)
headers = {
"Authorization": "Bearer {}".format(token),
"Content-Type": "application/octet-stream",
"Dropbox-API-Arg": arg_s
}
print(headers)
输出:
{'Authorization': 'Bearer abc', 'Content-Type': 'application/octet-stream', 'Dropbox-API-Arg': '{"path": "/my/path/This is the file_name", "mode": {"tag": "overwrite"}}'}