JSON格式化通过将字典值附加到列表来实现

2024-05-15 08:00:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个JSON对象,它是这样的:

{ "produktNr:"1234",
  "artNr_01":"12",
  "artNr_02":"23",
  "artNr_03":"",
  "artNr_04":"14",
  "name_01":"abc",
  "name_02":"der",
  "test":"junk"
}

我想把它转换成这样的字典:

{ "produktNr:"1234", "artNr":["12","23","","14"], "name":["abc","der"], "test":"junk"}

这种转换是基于给定的序列,例如seq = ["artNr","name"]。因此,在字典的键中搜索序列的内容,并将值收集到一个列表中。你知道吗

我目前的尝试:

tempDict = {}
for key,value in fmData.iteritems():
    for seqval in seq:
        if seqval in key:
            if seqval in tempDict:
                tempDict[seqval].append(value)
            else:
                x = []
                x.append(value)
                tempDict[seqval]=x
        else:
            tempDict[key] = value

面临一些问题。你知道吗

  • 值列表没有顺序,即"artNr":["","14","12","23"] 而不是[_01,_02,_03,_04]

  • 无法从字典中弹出项,因为在循环中无法删除字典项,从而导致:

    {“produktNr:”1234“,”artNr“:[”12“,”23“,”14“],”artNr\u 01“:”12“,”artNr\u 02“:”23“,”artNr\u 03“,”artNr\u 04“:”14“,”name“:[”abc“,”der“,”name\u 01“:”abc“,”name\u 02“:”der“,”test“:”junk“}

我很想知道如何处理这个问题,特别是如果有一个Python的方法来解决这个问题。你知道吗


Tags: keynameintest字典value序列seq
3条回答

您可以使用OrderedDict包中的collections

from collections import OrderedDict
import re

input_dict = { "produktNr":"1234",
               "artNr_01":"12",
               "artNr_02":"23",
               "artNr_03":"",
               "artNr_04":"14",
               "name_01":"abc",
               "name_02":"der",
               "test":"junk" }

# split keys on the first '_'
m = re.compile('^([^_]*)_(.*)')

def _order_by( item ):
    # helper function for ordering the dict.
    # item is split on first '_' and, if it was successful 
    # the second part is returned otherwise item is returned
    # if key is something like artNr_42, return 42
    # if key is something like test, return test 
    k,s = item
    try:
        return m.search(k).group(2)
    except:
        return k

# create ordered dict using helper function
orderedDict = OrderedDict( sorted(input_dict.items(), key=_order_by))

aggregated_dict = {}
for k, v in orderedDict.iteritems():
    # split key
    match = m.search(k)

    if match:
        # key is splittable, i.e., key is something like artNr_42
        kk = match.group(1)
        if kk not in aggregated_dict:
            # create list and add value
            aggregated_dict[kk] = [v]
        else:
            # add value
            aggregated_dict[kk].append(v)
    else:
        # key is not splittable, i.e., key is something like produktNr
        aggregated_dict[k] = v

print(aggregated_dict)

从而得到所需的输出

{'produktNr': '1234', 'test': 'junk', 'name': ['abc', 'der'], 'artNr': ['12', '23', '', '14']}

您可以重新创建一个新的字典,该字典将使用列表中键中的'_'对键的值进行分组,而其他键和值则保持不变。这应该做到:

d = { "produktNr":"1234", "artNr_01":"12", "artNr_02":"23","artNr_03":"","artNr_04":"14","name_01":"abc","name_02":"der","test":"junk"}
new_d= {}
for k, v in d.items():
    k_new = k.split('_')[0]
    if '_' in k:
        if k_new not in new_d:
            new_d[k_new] = [v]
        else:
            new_d[k_new].append(v)
    else:
        new_d[k_new] = v
print(new_d)
# {'artNr': ['', '14', '23', '12'], 'test': 'junk', 'produktNr': '1234', 'name': ['der', 'abc']}

dict是unordered集合,因此值附加到列表的顺序将是不确定的。你知道吗

对代码稍作修改:

tempDict = {}
for key,value in fmData.iteritems():
    seqval_in_key = "no"
    for seqval in seq:
        if seqval in key:
            seqval_in_key = "yes"
    for seqval in seq:
        if seqval in key:
            if seqval in tempDict:
                tempDict[seqval].append(value)
            else:
                x = []
                x.append(value)
                tempDict[seqval]=x
        else:
            if (seqval_in_key == "no"):
                tempDict[key] = value
print tempDict

结果:

{'produktNr': '1234', 'test': 'junk', 'name': ['abc', 'der'], 'artNr': ['14', '23', '', '12']}

相关问题 更多 >