如何将Unicode dict转换为dict

我正在尝试转换:


datalist = [u"{gallery: 'gal1', smallimage: 'http://www.styleever.com/media/catalog/product/cache/1/small_image/445x370/17f82f742ffe127f42dca9de82fb58b1/2/_/2_12.jpg',largeimage: 'http://www.styleever.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/2/_/2_12.jpg'}",

 u"{gallery: 'gal1', smallimage: 'http://www.styleever.com/media/catalog/product/cache/1/small_image/445x370/17f82f742ffe127f42dca9de82fb58b1/3/_/3_13.jpg',largeimage: 'http://www.styleever.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/3/_/3_13.jpg'}",

 u"{gallery: 'gal1', smallimage: 'http://www.styleever.com/media/catalog/product/cache/1/small_image/445x370/17f82f742ffe127f42dca9de82fb58b1/5/_/5_3_1.jpg',largeimage: 'http://www.styleever.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/5/_/5_3_1.jpg'}",

 u"{gallery: 'gal1', smallimage: 'http://www.styleever.com/media/catalog/product/cache/1/small_image/445x370/17f82f742ffe127f42dca9de82fb58b1/1/_/1_22.jpg',largeimage: 'http://www.styleever.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/1/_/1_22.jpg'}",

 u"{gallery: 'gal1', smallimage: 'http://www.styleever.com/media/catalog/product/cache/1/small_image/445x370/17f82f742ffe127f42dca9de82fb58b1/4/_/4_7_1.jpg',largeimage: 'http://www.styleever.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/4/_/4_7_1.jpg'}"]

要列出包含python dict的列表。如果我尝试使用关键字提取值,则会出现此错误:


for i in datalist:

    print i['smallimage']

   ....:     


---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-20-686ea4feba66> in <module>()

      1 for i in datalist:

----> 2     print i['smallimage']

      3 


TypeError: string indices must be integers

如何将包含Unicode Dict的列表转换为Dict ..


萧十郎
浏览 222回答 3
3回答

慕哥9229398

您可以使用具有非严格模式的demjson模块来处理您拥有的数据:import demjsonfor data in datalist:&nbsp; &nbsp; dct = demjson.decode(data)&nbsp; &nbsp; print dct['gallery'] # etc...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python