Python 需要将变量值作为值传递到字典中
我已将一个值存储到变量中。下面是-Python 需要将变量值作为值传递到字典中,python,dictionary,Python,Dictionary,我已将一个值存储到变量中。下面是- itemID = item['witId'] print "new ID is :" +str(itemID) 现在,我想将itemId的值传递到以下字典中: data = '{"witIds": ["itemID "], "widgetExpiryDate":"12.06.2015 09:12:00.UTC" , "firstAlertDate":"11.06.2015 04:06:00.UTC","secondAlertDate":"11.06.201
itemID = item['witId']
print "new ID is :" +str(itemID)
现在,我想将itemId的值传递到以下字典中:
data = '{"witIds": ["itemID "], "widgetExpiryDate":"12.06.2015 09:12:00.UTC" , "firstAlertDate":"11.06.2015 04:06:00.UTC","secondAlertDate":"11.06.2015 05:02:00.UTC" }'
此代码向我显示
itemID
无效。我想这就是您想要的:
itemID=item[“witId”]
数据={“widgetExpiryDate”:“12.06.2015 09:12:00.UTC”,“firstAlertDate”:“11.06.2015 04:06:00.UTC”,“secondAlertDate”:“11.06.2015 05:02:00.UTC”}
数据[“witIds”]=[itemID]
如果您试图将该JSON转换为字典,则可以使用JSON
模块:
导入json
数据=“{”widgetExpiryDate:“12.06.2015 09:12:00.UTC”,“firstAlertDate:“11.06.2015 04:06:00.UTC”,“secondAlertDate:“11.06.2015 05:02:00.UTC”}”
加载(数据)
那……看起来不像字典。对我来说,这看起来像json。你的itemID
中有什么?原始项
字典是什么?如果您试图从dict获取一个它没有的密钥,然后打印它,它将出错。另外,当您设置新的数据
dict时,您不需要在整件事情周围加引号,如果您想在其中添加变量itemID
,则不应该在其周围加引号。