【问题标题】:error in converting json to a dictionary in python在python中将json转换为字典时出错
【发布时间】:2013-09-24 22:55:06
【问题描述】:
for i in products.iter():
    print i

这是我运行的代码,输出如下:

localhost:desktop siddharth$ python reader.py 
{u'sem3_id': u'1IngGhlIpoOGYCiMaEASSQ', u'cat_id': u'12151', u'updated_at': 1379589873, u'height': u'213.36', u'offers_total': 13, u'category': u'Inkjet Printers', u'price_currency': u'USD', u'sem3_help': u'To view image links for this product, please upgrade your plan.', u'width': u'281.94', u'sitedetails': [{u'sku': u'B00005AL05', u'latestoffers': [{u'price': u'366.99', u'shipping': u'19.98', u'seller': u'Buy4Less Now!', u'currency': u'USD', u'lastrecorded_at': 1379589800, u'firstrecorded_at': 1377106600, u'id': u'1BA5umTG80Gkg0oi40kiOu', u'condition': u'New'}, {u'price': u'379.96', u'shipping': u'12.49', u'seller': u'Battle Distribution, LLC', u'currency': u'USD', u'lastrecorded_at': 1379589800, u'firstrecorded_at': 1374975300, u'id': u'05cPhKOvouWkGc08sKaseq', u'condition': u'New'}, {u'price': u'99.99', u'shipping': u'12.49', u'seller': u'clabro', u'currency': u'USD', u'lastrecorded_at': 1379589800, u'firstrecorded_at': 1365164600, u'id': u'7aUtWYQlLkGuygGeWOgKWu', u'condition': u'Used - Very Good'}], u'name': u'amazon.com', u'url': u'http://www.amazon.com/dp/B00005AL05', u'offers_count': 13, u'recentoffers_count': 5}], u'price': u'366.99', u'gtins': [u'00033584113910'], u'ean': u'0033584113910', u'mpn': u'C408011', u'brand': u'Epson', u'geo': [u'usa'], u'manufacturer': u'Epson', u'name': u'Epson Stylus Photo 785EPX Inkjet Printer', u'created_at': 1347483414, u'upc': u'033584113910', u'length': u'264.16', u'model': u'C408011'}

我如何使它成为一个 python dict?

我尝试从上面使用的迭代器中生成列表理解,然后执行 json.loads(listcomprehension) 但这给了我错误。

【问题讨论】:

    标签: python json dictionary


    【解决方案1】:

    列表以[ 开头,可以,但随后以) 结尾。将其更改为]() 用于元组。

    >>> import json
    >>> json.loads('[{"name":"same"}]')
    [{u'name': u'same'}]
    

    【讨论】:

    • @garciaj Heh,别担心,我们都有那个时刻
    • 嘿 haidro,你如何区分 json 字符串和 python 字典?
    • @garciaj 一个是字符串,一个是字典;)。 type(data) 会告诉你哪个是哪个。
    • haidro ive 编辑了问题,请您看一下
    • @garciaj 你所拥有的对我来说就像一本字典。这是打印东西的结果吗?我需要看看你的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 2011-01-29
    • 2019-04-19
    • 2019-01-16
    • 2020-08-08
    • 2017-07-02
    相关资源
    最近更新 更多