Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中将dict的键映射到json_Python_Python 3.x - Fatal编程技术网

如何在python中将dict的键映射到json

如何在python中将dict的键映射到json,python,python-3.x,Python,Python 3.x,我正在从excel工作表中提取数据,并将其存储在字典变量中,例如: data = {"customer_name":"XYZ", "customer_mobile":"989898", "customer_country":"country"} 现在我需要调用rest api来创建客户,需要以json格式发送数据,如下所示: { "custName": "string", "custMobile": "string", "custCountry":"string", } dict

我正在从excel工作表中提取数据,并将其存储在
字典
变量中,例如:

data = {"customer_name":"XYZ", "customer_mobile":"989898", "customer_country":"country"}
现在我需要调用rest api来创建客户,需要以json格式发送数据,如下所示:

{
  "custName": "string",
  "custMobile": "string",
  "custCountry":"string",
}
dictionary
json
中的键是不同的,不能更改它们中的任何一个,因此如何映射这两个键

谢谢这有帮助吗

data = {"customer_name":"XYZ", "customer_mobile":"989898", "customer_country":"country"}

dataToSend = {
    "custName" : data["customer_name"],
    "custMobile": data["customer_mobile"],
    "custCountry":data["customer_country"],
}

print(dataToSend)
我正在创建一个单独的对象
dataToSend
,并从包含excel工作表中的值的数据对象填充其值

谢谢这有帮助吗

data = {"customer_name":"XYZ", "customer_mobile":"989898", "customer_country":"country"}

dataToSend = {
    "custName" : data["customer_name"],
    "custMobile": data["customer_mobile"],
    "custCountry":data["customer_country"],
}

print(dataToSend)
我正在创建一个单独的对象
dataToSend
,并从包含excel工作表中的值的数据对象填充其值


谢谢

这是可能对您有所帮助的代码示例

导入json
def转换(word):
word=word.replace('customer','cust'))
返回“”。join(x.capitalize()或''.'表示word.split('.'')中的x)
数据={“客户名称”:“XYZ”,“客户移动”:“989898”,“客户国家”:“国家”}
keys=data.keys()
对于i键:
data[convert(i)]=data.pop(i)
r=json.dumps(数据)

print(json.loads(r))
这是可能对您有所帮助的代码示例

导入json
def转换(word):
word=word.replace('customer','cust'))
返回“”。join(x.capitalize()或''.'表示word.split('.'')中的x)
数据={“客户名称”:“XYZ”,“客户移动”:“989898”,“客户国家”:“国家”}
keys=data.keys()
对于i键:
data[convert(i)]=data.pop(i)
r=json.dumps(数据)
print(json.loads(r))
函数用于更改字典的所有键

api=["custName","custMobile","custCountry"]
data=dict(zip(api, list(data.values()))) 
函数用于更改字典的所有键

api=["custName","custMobile","custCountry"]
data=dict(zip(api, list(data.values()))) 

您可以对照两个键中的公共值进行检查:

data = {"customer_name":"XYZ", "customer_mobile":"989898", "customer_country":"country"}
api = { "custName": "string", "custMobile": "string", "custCountry":"string",}

for key in data.keys():
    for api_key in api.keys():
        if key[-3:] == api_key[-3:]:
            api[api_key] = data[key]
print(api)

>>> {"custName": "XYZ", "custMobile": "989898", "custCountry":"country"}

检查每个字典中每个键的最后3个字符,您可以比较和匹配键。

您可以对照两个键中的公共值进行检查:

data = {"customer_name":"XYZ", "customer_mobile":"989898", "customer_country":"country"}
api = { "custName": "string", "custMobile": "string", "custCountry":"string",}

for key in data.keys():
    for api_key in api.keys():
        if key[-3:] == api_key[-3:]:
            api[api_key] = data[key]
print(api)

>>> {"custName": "XYZ", "custMobile": "989898", "custCountry":"country"}
检查每个字典中每个键的最后3个字符,可以比较和匹配键