将字符串转换为字典列表Python3

时间:2017-01-12 16:22:08

标签: python list dictionary

使用Python 3.5.2,将字符串转换为字典列表的最佳方法是什么?

我正在抓取一个网站,并将以下内容作为长度为1的列表返回:

(为便于阅读而格式化)

[
{"variation_id":573,"variation_is_visible":true,"variation_is_active":true,"is_purchasable":true,"display_price":1099,"display_regular_price":1099,"attributes":{"attribute_pa_size":"king"},"image_src":"","image_link":"","image_title":"","image_alt":"","image_caption":"","image_srcset":"","image_sizes":"","price_html":"<span class=\"price\"><span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&#82;<\/span>1,099.00<\/span><\/span>","availability_html":"<p class=\"stock in-stock\">2 in stock<\/p>","sku":"6006239211693","weight":" kg","dimensions":"","min_qty":1,"max_qty":2,"backorders_allowed":false,"is_in_stock":true,"is_downloadable":false,"is_virtual":false,"is_sold_individually":"no","variation_description":""},

{"variation_id":574,"variation_is_visible":true,"variation_is_active":true,"is_purchasable":true,"display_price":989,"display_regular_price":989,"attributes":{"attribute_pa_size":"queen"},"image_src":"","image_link":"","image_title":"","image_alt":"","image_caption":"","image_srcset":"","image_sizes":"","price_html":"<span class=\"price\"><span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&#82;<\/span>989.00<\/span><\/span>","availability_html":"<p class=\"stock in-stock\">2 in stock<\/p>","sku":"6006239211686","weight":" kg","dimensions":"","min_qty":1,"max_qty":2,"backorders_allowed":false,"is_in_stock":true,"is_downloadable":false,"is_virtual":false,"is_sold_individually":"no","variation_description":""},

{"variation_id":575,"variation_is_visible":true,"variation_is_active":true,"is_purchasable":true,"display_price":949,"display_regular_price":949,"attributes":{"attribute_pa_size":"double"},"image_src":"","image_link":"","image_title":"","image_alt":"","image_caption":"","image_srcset":"","image_sizes":"","price_html":"<span class=\"price\"><span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&#82;<\/span>949.00<\/span><\/span>","availability_html":"<p class=\"stock in-stock\">2 in stock<\/p>","sku":"6006239211679","weight":" kg","dimensions":"","min_qty":1,"max_qty":2,"backorders_allowed":false,"is_in_stock":true,"is_downloadable":false,"is_virtual":false,"is_sold_individually":"no","variation_description":""}

]

我尝试将其转换为str,将其分配给&#39;然后使用json.loads(s),但那不起作用。

我希望有一个列表对象,我可以通过以下方式访问值:

for item in form_data_returned:
    print item['variation_id']  # prints 573  574  575

由于

2 个答案:

答案 0 :(得分:2)

from collections import defaultdict

# Set aliases for `true` and `false` in the output so
# we won't get NameError exceptions thrown.
true = True
false = False

raw = [
{"variation_id":573,"variation_is_visible":true,"variation_is_active":true,"is_purchasable":true,"display_price":1099,"display_regular_price":1099,"attributes":{"attribute_pa_size":"king"},"image_src":"","image_link":"","image_title":"","image_alt":"","image_caption":"","image_srcset":"","image_sizes":"","price_html":"<span class=\"price\"><span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&#82;<\/span>1,099.00<\/span><\/span>","availability_html":"<p class=\"stock in-stock\">2 in stock<\/p>","sku":"6006239211693","weight":" kg","dimensions":"","min_qty":1,"max_qty":2,"backorders_allowed":false,"is_in_stock":true,"is_downloadable":false,"is_virtual":false,"is_sold_individually":"no","variation_description":""},

{"variation_id":574,"variation_is_visible":true,"variation_is_active":true,"is_purchasable":true,"display_price":989,"display_regular_price":989,"attributes":{"attribute_pa_size":"queen"},"image_src":"","image_link":"","image_title":"","image_alt":"","image_caption":"","image_srcset":"","image_sizes":"","price_html":"<span class=\"price\"><span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&#82;<\/span>989.00<\/span><\/span>","availability_html":"<p class=\"stock in-stock\">2 in stock<\/p>","sku":"6006239211686","weight":" kg","dimensions":"","min_qty":1,"max_qty":2,"backorders_allowed":false,"is_in_stock":true,"is_downloadable":false,"is_virtual":false,"is_sold_individually":"no","variation_description":""},

{"variation_id":575,"variation_is_visible":true,"variation_is_active":true,"is_purchasable":true,"display_price":949,"display_regular_price":949,"attributes":{"attribute_pa_size":"double"},"image_src":"","image_link":"","image_title":"","image_alt":"","image_caption":"","image_srcset":"","image_sizes":"","price_html":"<span class=\"price\"><span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&#82;<\/span>949.00<\/span><\/span>","availability_html":"<p class=\"stock in-stock\">2 in stock<\/p>","sku":"6006239211679","weight":" kg","dimensions":"","min_qty":1,"max_qty":2,"backorders_allowed":false,"is_in_stock":true,"is_downloadable":false,"is_virtual":false,"is_sold_individually":"no","variation_description":""}

]

# keys being a set ensures that every key occurs only once.
keys = set()

# Initializing form_data_returned as a defaultdict allows
# us to access keys that are not already in form_data_returned.
# For example form_data_returned['weight'].append('kg') would throw
# KeyError exception for an empty form_data_returned had we declared
# it as a normal dict().
form_data_returned = defaultdict(list)

for dictionary in raw:
    keys.update(dictionary.keys())
    for key in keys:
        form_data_returned[key].append(dictionary[key])

我们现在可以按键检索数据:

print(form_data_returned['variation_id'])
>>> [573, 574, 575]

答案 1 :(得分:0)

使用re模块预处理字符串,然后使用json模块将其解析为字典。

假设您已将数据转换为字符串,并且您知道某些规则适用于内容*,您可以尝试以下操作:

str = '...'

escaped = re.sub('(?<=[^,:{}])(\\\")(?=[^,:{}])','\\"',str)

dict = json.loads(escaped)

正则表达式(?<=[^,:{}])(\\\")(?=[^,:{}])将解析字符串,并标识所有不在"之后或后跟',' , ':' , '{', '}'的字符",以便(?<=[^,:{}])在数据中的字符串可以正确转义。

*规则我的意思是,您必须知道,使用的正则表达式找到正确的字符 - 如果数据源可以提供一致性,则上面的代码应该有效(扩展(?=[^,:{}])和{{ 1}}具有匹配所有数据的必要字符的部分