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

2024-09-30 04:31:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我已将一个值存储到一个变量中。下面是-

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是无效的。你知道吗


Tags: idnewdata字典isitemutcprint
1条回答
网友
1楼 · 发布于 2024-09-30 04:31:54

我想这就是你想要的:

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)

相关问题 更多 >

    热门问题