python
  • dictionary
  • 2015-07-20 72 views -3 likes 
    -3

    我已将值存储到变量中。下面是觉得─需要将变量值作为值传递给字典

    itemID = item['witId'] 
    print "new ID is :" +str(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无效。

    +3

    这....看起来不像字典。这对我来说就像json。 – NightShadeQueen

    +0

    你的'itemID'里有什么? – dlask

    +0

    什么是原始'item'字典?如果你试图从它没有的字典中获得一个密钥,然后打印它,它会出错。另外,当你设置新的'data'字典时,你不需要引用整个事物,如果你想添加变量'itemID',你不应该引用它。 – Buzz

    回答

    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) 
    
    相关问题