需要将变量值作为值传递到字典中

时间:2015-07-20 13:13:05

标签: 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.2015 05:02:00.UTC"   }'

此代码显示itemID无效。

1 个答案:

答案 0 :(得分:1)

我认为这就是你想要的:

itemID = item["witId"]
data = {"widgetExpiryDate":"12.06.2015 09:12:00.UTC" , "firstAlertDate":"11.06.2015 04:06:00.UTC","secondAlertDate":"11.06.2015 05:02:00.UTC"}
data["witIds"] = [itemID]

如果您尝试将该JSON转换为字典,则可以使用json模块:

import json
data = '{"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.loads(data)