dict值相互叠加

时间:2016-11-01 01:12:39

标签: python json

假设我的场景有2个项目,名为arte_item1arte_item2 .. 我设法检查了项目中的一些属性,这些属性检查了'键'在我的这个json文件中。 目前我遇到了问题,试图将数据附加到字典中

我的json文件如下:

{
    "common": {
        "yellow": [
            "SPEC 0.723",
            "RGB 0.002 0.002 0.002",
            "OCC 0.8 1.2"
        ], 
        "blue": [
            "SPEC 0.9",
            "RGB 0.014 0.019 0.047",
            "OCC 0.345"
        ]
    },
    "uncommon":{
        "blue-ish green": [
            "SPEC 0.9",
            "RGB 0.014 0.019 0.047",
            "OCC 0.345"
        ],
        "gray": [
            "SPEC 1",
            "RGB 0.012 0.012 0.012",
            "OCC 0.4"
        ], 
        "black": [
            "SPEC 0.9", 
            "RGB 0.067 0.067 0.067",
            "OCC 0.4" 
        ]
    },
    "rare": {
        "gold": [
            "SPEC 0.723",
            "RGB 0.002 0.002 0.002",
            "OCC 0.3"
        ], 
        "silver": [
            "SPEC 1",
            "RGB 0.349 0.349 0.349",
            "OCC 0.3"
        ]
    }
}

虽然我期待我的dict输出类似于:

 {"arte1" : {u'gold': [u'RGB', u'0.002', u'0.002', u'0.002'], u'silver': [u'RGB', u'0.349', u'0.349', u'0.349']}, 
  "arte2" : {u'blue-ish green': [u'RGB', u'0.014', u'0.019', u'0.047'], u'gray': [u'RGB', u'0.012', u'0.012', u'0.012'], u'black': [u'RGB', u'0.067', u'0.067', u'0.067']}}

我得到了以下结果,其中颜色值全部附加在一起,就像是堆积在一起的'在彼此..

{"arte1" : {u'gold': [u'RGB', u'0.002', u'0.002', u'0.002'], u'silver': [u'RGB', u'0.349', u'0.349', u'0.349'], u'blue-ish green': [u'RGB', u'0.014', u'0.019', u'0.047'], u'gray': [u'RGB', u'0.012', u'0.012', u'0.012'], u'black': [u'RGB', u'0.067', u'0.067', u'0.067']},
 "arte2" : {u'gold': [u'RGB', u'0.002', u'0.002', u'0.002'], u'silver': [u'RGB', u'0.349', u'0.349', u'0.349'], u'blue-ish green': [u'RGB', u'0.014', u'0.019', u'0.047'], u'gray': [u'RGB', u'0.012', u'0.012', u'0.012'], u'black': [u'RGB', u'0.067', u'0.067', u'0.067']}}

以下是我的代码:

# arte_item1 -> gives the key 'rare'
# arte_item2 -> gives the key 'uncommon'

def read_json(file_path):
    with open(file_path) as data_file:
        data = json.load(data_file)I
        return data

    get_color_info = {}
    get_full_info = {}

    # possibilities comes from read_json(), so in this case, it will be data['rare'] and data['uncommon']
    for index, names in table_data:
        ...
        if not names  is None:
            ...
        for name, color in possibilities.items():
            for item in color:
                if "RGB" in item:
                    get_color_info[name] = item.replace(' ', ',').split(',')
                    get_full_info[item_name] = get_color_info

非常感谢有人能给我反馈

0 个答案:

没有答案