提供Interface=['1/1/1', '1/2/3'], tag=11
的参数,我需要将缺少的界面添加到json_data[Interface]
和json_data[Port]
,如下所示。除name
值以外的所有地点都使用'%2F'
作为'/'
。
输入json:
{
"Interface": {
"1%2F1%2F1": {
"name": "1/1/1",
}
},
"Port": {
"1%2F1%2F1": {
"interfaces": [
"1%2F1%2F1"
],
"name": "1/1/1",
"tag": "10"
}
}
}
输出json应如下所示:
{
"Interface": {
"1%2F1%2F1": {
"name": "1/1/1",
},
"1%2F2%2F3": {
"name": "1/2/3",
}
},
"Port": {
"1%2F1%2F1": {
"interfaces": [
"1%2F1%2F1"
],
"name": "1/1/1",
"tag": "10"
},
"1%2F2%2F3": {
"interfaces": [
"1%2F2%2F3"
],
"name": "1/2/3",
"tag": "11"
}
}
}
我尝试使用迭代界面列表:
for item in interface:
if item is not in json_data["Interface"].keys():
json_data["Interface"][item] = { "name" : item }
但是以错误的格式返回:
"Interface": {
"2": {
"name": "2"
},
"1": {
"name": "1"
},
"'": {
"name": "'"
},
" ": {
"name": " "
},
",": {
"name": ","
},
"/": {
"name": "/"
},
答案 0 :(得分:0)
如何处理斜杠和%2F
以下是从/
转换为%2F
的简单方法:
'1/1/1'.replace('/', '%2F')
# Results: '1%2F1%2F1'
我不确定为什么你需要这种格式,但如果它与通过URL传递相关而你需要转义其他字符,你可以使用:
import urllib
urllib.parse.quote('1/1/1', safe='')
# Results: '1%2F1%2F1'
您的输出
看起来你不小心迭代了一串字母而不是接口列表。你是如何分配interface
变量的?
检查现有界面
if item is not in json_data["Interface"].keys()
如果item
直接来自您的输入格式为1/1/1
,则json_data["Interface"]
无效,因为# Group the local category_feature_product.id, local feature value -
# and external feature value for the product together.
GROUP_CONCAT(
CONCAT(cfpt.category_feature_product_id, '||', pcfpt.category_feature_product_id, '||', cfpt.value, '||', pcfpt.value)
SEPARATOR ';'
) AS mysql_category_feature_products
中的所有密钥都使用转义格式。