【问题标题】:Python parse string as dictionaryPython将字符串解析为字典
【发布时间】:2018-12-26 13:16:54
【问题描述】:

我有以下字符串:

"[['Categories', [['180972'], ['180800'], ['16228'], ['32733'], ['32789'], ['32833'], ['325137'], ['32839'], ['25329'], ['42605'], ['428240849'], ['5101'], ['568'], ['570716'], ['57116'], ['57080545404'], ['57083134076']]], ['Tags', ['Stock', 'Color', 'Fam', 'Dress','Maxi']], ['Type', ['Luxary']], ['Vendor', ['AAA']]]"

我想将其解析为 dict/json。 最好的方法是什么?

【问题讨论】:

  • @Graipher 这是一个字符串,看我的编辑
  • @Aprillion: 这似乎是一个无效的 JSON 字符串...
  • @Graipher 简单地将' 替换为"

标签: python json string parsing dictionary


【解决方案1】:

您可以使用ast.literal_eval 评估字符串并返回 Python 对象(如果语法正确)。 Using this is safer than using eval.

import ast

s = "[['Categories', [['180972'], ['180800'], ['16228'], ['32733'], ['32789'], ['32833'], ['325137'], ['32839'], ['25329'], ['42605'], ['428240849'], ['5101'], ['568'], ['570716'], ['57116'], ['57080545404'], ['57083134076']]], ['Tags', ['Stock', 'Color', 'Fam', 'Dress','Maxi']], ['Type', ['Luxary']], ['Vendor', ['AAA']]]"

l = ast.literal_eval(s)
d = dict(l)
{'Categories': [['180972'],
  ['180800'],
  ['16228'],
  ['32733'],
  ['32789'],
  ['32833'],
  ['325137'],
  ['32839'],
  ['25329'],
  ['42605'],
  ['428240849'],
  ['5101'],
  ['568'],
  ['570716'],
  ['57116'],
  ['57080545404'],
  ['57083134076']],
 'Tags': ['Stock', 'Color', 'Fam', 'Dress', 'Maxi'],
 'Type': ['Luxary'],
 'Vendor': ['AAA']}

如果您还想摆脱内部列表,请使用the other answer,而不是仅在对象上调用dict

【讨论】:

    【解决方案2】:

    试试这个把它转换成dict:

    data= "[['Categories', [['180972'], ['180800'], ['16228'], ['32733'], ['32789'], ['32833'], ['325137'], ['32839'], ['25329'], ['42605'], ['428240849'], ['5101'], ['568'], ['570716'], ['57116'], ['57080545404'], ['57083134076']]], ['Tags', ['Stock', 'Color', 'Fam', 'Dress','Maxi']], ['Type', ['Luxary']], ['Vendor', ['AAA']]]"
    
    data = eval(data)
    
    d={}
    for i in data:
        d[i[0]] = [x for x, in i[1]] if isinstance(i[1][0], list) else i[1]
    

    输出将是:

    {'Categories': 
      ['180972',
       '180800',
       '16228',
       '32733',
       '32789',
       '32833',
       '325137',
       '32839',
       '25329',
       '42605',
       '428240849',
       '5101',
       '568',
       '570716',
       '57116',
       '57080545404',
       '57083134076'],
     'Tags': ['Stock', 'Color', 'Fam', 'Dress', 'Maxi'],
     'Type': ['Luxary'],
     'Vendor': ['AAA']
    }
    

    【讨论】:

    • 仅在您自己创建或自己确认安全的文本上使用eval切勿来自未知或不受信任的来源。
    【解决方案3】:

    这个怎么样

    >>> import itertools
    >>> import ast
    >>> import pprint
    >>> i = ast.literal_eval(s)
    >>> d = {k[0]:list(itertools.chain(*k[1])) if isinstance(k[1][0], list) else list(k[1]) for k in i}
    >>> pprint.pprint(d)
    {'Categories': ['180972',
                    '180800',
                    '16228',
                    '32733',
                    '32789',
                    '32833',
                    '325137',
                    '32839',
                    '25329',
                    '42605',
                    '428240849',
                    '5101',
                    '568',
                    '570716',
                    '57116',
                    '57080545404',
                    '57083134076'],
     'Tags': ['Stock', 'Color', 'Fam', 'Dress', 'Maxi'],
     'Type': ['Luxary'],
     'Vendor': ['AAA']}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      相关资源
      最近更新 更多