1

我正在编写一个类来获取 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方法中提取每个相关书签文件夹下的书签 url。

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

我试过用字符串建立字典路径。我想我需要传入一个元组或键列表来循环并动态构建路径,但我无法让它工作,而且我可怜的脑袋坏了!

有一个类似的问题,但使用的答案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'))
4

1 回答 1

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]
于 2013-11-13T11:10:16.127 回答