如何在python中动态构建嵌套字典的路径/引用

时间:2013-11-13 10:50:11

标签: python json dictionary

我正在编写一个类来获取Chrome书签文件(请参阅下面的示例):

{
   "checksum": "452bebcad611a3faffb2c009099139e5",
   "roots": {
      "bookmark_bar": {
         "children": [ {
            "date_added": "13028719861473329",
            "id": "4",
            "name": "first bookmark",
            "type": "url",
            "url": "chrome://newtab/"
         }, {
            "children": [ {
               "children": [ {
                  "date_added": "13026904508000000",
                  "id": "7",
                  "name": "Getting Started",
                  "type": "url",
                  "url": "https://www.mozilla.org/en-GB/firefox/central/"
               } ],
               "date_added": "13028740260032410",
               "date_modified": "0",
               "id": "6",
               "name": "Bookmarks Toolbar",
               "type": "folder"
            }, {
               "children": [ {
                  "date_added": "13026904508000000",
                  "id": "9",
                  "name": "Help and Tutorials",
                  "type": "url",
                  "url": "https://www.mozilla.org/en-GB/firefox/help/"
               }, {
                  "date_added": "13026904508000000",
                  "id": "10",
                  "name": "Customise Firefox",
                  "type": "url",
                  "url": "https://www.mozilla.org/en-GB/firefox/customize/"
               }, {
                  "date_added": "13026904508000000",
                  "id": "11",
                  "name": "Get Involved",
                  "type": "url",
                  "url": "https://www.mozilla.org/en-GB/contribute/"
               }, {
                  "date_added": "13026904508000000",
                  "id": "12",
                  "name": "About Us",
                  "type": "url",
                  "url": "https://www.mozilla.org/en-GB/about/"
               } ],
               "date_added": "13028740260032410",
               "date_modified": "0",
               "id": "8",
               "name": "Mozilla Firefox",
               "type": "folder"
            }, {
               "date_added": "13026904551000000",
               "id": "13",
               "name": "Welcome to Firefox",
               "type": "url",
               "url": "http://www.mozilla.org/en-US/firefox/24.0/firstrun/"
            } ],
            "date_added": "13028740260004410",
            "date_modified": "0",
            "id": "5",
            "name": "Imported From Firefox",
            "type": "folder"
         } ],
         "date_added": "13028719626916276",
         "date_modified": "13028719861473329",
         "id": "1",
         "name": "Bookmarks bar",
         "type": "folder"
      },
      "other": {
         "children": [  ],
         "date_added": "13028719626916276",
         "date_modified": "0",
         "id": "2",
         "name": "Other bookmarks",
         "type": "folder"
      },
      "synced": {
         "children": [  ],
         "date_added": "13028719626916276",
         "date_modified": "0",
         "id": "3",
         "name": "Mobile bookmarks",
         "type": "folder"
      }
   },
   "version": 1
}

我将JSON转换为嵌套字典,然后在write_data方法中提取每个相关书签文件夹下的书签网址。

由于每个文件夹中可以嵌套任意数量的书签文件夹和/或书签,我想在自身内部调用write_data方法,以便每次找到嵌套文件夹时都会继续提取子数据。我只是无法弄清楚如何将相关的儿童词典传递给相同的方法。

我尝试用字符串构建字典路径。我想我需要传入一个元组或键列表来循环并动态构建路径,但我无法使它工作,我的可怜的头被破坏了!

有一个similar question,但答案使用了yield,这让我完全糊涂了,无论如何都不是一个完全有效的解决方案。请帮忙!

import json
import sys
import codecs


class FileExtractor(object):
    def __init__(self, input_file):
        self.infile = codecs.open(input_file, encoding='utf-8')
        self.bookmark_data = json.load(self.infile)

    def write_data(self, my_key):

        for key, value in self.bookmark_data[my_key].iteritems():
            if type(self.bookmark_data[my_key][key]) is dict:
                print self.bookmark_data[my_key][key]['name']
                for subkey, subvalue in self.bookmark_data[my_key][key].iteritems():
                    if subkey == "children" and len(self.bookmark_data[my_key][key][subkey]) <> 0:
                        print "this is a child. I can't figure out how to use write_data with this"
                        #self.write_data('[my_key][key][subkey]')               
                    else:
                        print subkey, ": ",  self.bookmark_data[my_key][key][subkey]

if(__name__=="__main__"):
    stuff= FileExtractor(sys.argv[1])

    stuff.write_data(('roots'))

1 个答案:

答案 0 :(得分:0)

不确定这是否是你所得到的,但是如果你将字典对象本身传递给write_data而不是密钥,你可以在字典过程中尽可能地递归。警告我还没有对此进行过测试,只是为了给你一个想法。

def write_data(self, my_dict=None):
    my_dict = my_dict or self.bookmark_data['roots']
    for key, value in my_dict.items():
        if type(my_dict[key]) is dict:
            print my_dict[key]['name']
            for subkey, subvalue in my_dict[key].items():
                if subkey == "children" and len(my_dict[key][subkey]) <> 0:
                    for child in my_dict[key][subkey]:
                        self.write_data(child)           
                else:
                    print subkey, ": ",  my_dict[key][subkey]

更好的版本:

def write_data(self, my_dict=None):
    my_dict = my_dict or self.bookmark_data['roots']
    if 'name' in my_dict:
        print my_dict['name']

    for key, value in my_dict.items():
        if type(my_dict[key]) is dict:
            self.write_data(my_dict[key])
        elif type(my_dict[key]) is list:
            for item in my_dict[key]:
                self.write_data(item)
        else:
            print key, ": ",  my_dict[key]