2017-01-19 182 views
1

我有4個項目。如何添加字典中的字典

item['bigCtgr'] = 'item' 
item['smaCtgr'] = 'food' 
item['ssCtgr'] = 'apple' 
item['sCtgr'] = 'red' 

我將多次添加到process_item。 所以我想讓這樣的結構。 喜歡的東西

{"item" : 
    {"food": 
     {"apple": 
      {"green":NULL}, 
      {"red":NULL}}, 
     {"banana": 
      {"yellow":NULL}, 
      {"green":NULL}}, 
    } 
    {"sweet": 
     {"candy": 
      {"yellow":NULL}} 
    } 
} 

類別,但我的代碼不能正常工作,我不知道爲什麼。

class CategoryPipeline(object): 
    global ctgr 
    ctgr = {} 

    def __init__(self): 
     global file 
     file = open("test.json","w") 

    def process_item(self, item, spider): 

     if item['bigCtgr'] not in ctgr.keys(): 
      ctgr[item['bigCtgr']] = {item['smaCtgr']: {item['ssCtgr'] : {item['sCtgr'] : 'NULL'}}} 
     if item['smaCtgr'] not in ctgr[item['bigCtgr']].keys(): 
      ctgr[item['bigCtgr']][item['smaCtgr']] = {item['ssCtgr']: {item['sCtgr'] : 'NULL'}} 
     elif item['ssCtgr'] not in ctgr[item['bigCtgr']][item['smaCtgr']].keys(): 
      ctgr[item['bigCtgr']][item['smaCtgr']][item['ssCtgr']] = {item['sCtgr'] : 'NULL'} 
     else: 
      ctgr[item['bigCtgr']][item['smaCtgr']][item['ssCtgr']][item['sCtgr']] = 'NULL' 


    def __del__(self): 
     b = json.dumps(ctgr, ensure_ascii=False).encode('utf-8') 
     file.write(b) 
     file.write('\n') 
     file.close() 

我該如何編碼?

+0

不太清楚,你想的瓦萊斯爲'NULL','bigCtgr'的意義是什麼等 –

+0

我只想使用僅字典的層次結構.. – Amily

+0

你可以把它作爲樣本輸入和期望的輸出,所以我可以更好地測試它和t ry給你一個答案 –

回答

2

我用dict和__missing__函數實現了一棵樹。這增加的節點,如果不存在

import json 

class CategoryNode(dict): 
    def __missing__(self,key): 
     self[key] = CategoryNode() 
     return self[key] 
    def add_item(self, item): 
     self[item['bigCtgr']][item['smaCtgr']][item['ssCtgr']][item['sCtgr']] = CategoryNode() 



class CategoryPipeline(object): 
    ctgr = CategoryNode() 
    file = "test.json" 

    def process_item(self, item, spider): 
     CategoryPipeline.ctgr.add_item(item) 

    def json(self): 
     json.dump(CategoryPipeline.ctgr,open(CategoryPipeline.file,'w'), ensure_ascii=False, encoding='utf-8') 

呢?這就是你爲什麼要用它

cp = CategoryPipeline() 
item = {} 
item['bigCtgr'] = 'item' 
item['smaCtgr'] = 'food' 
item['ssCtgr'] = 'apple' 
item['sCtgr'] = 'red' 
item2 = {} 
item2['bigCtgr'] = 'item' 
item2['smaCtgr'] = 'food' 
item2['ssCtgr'] = 'Orange' 
item2['sCtgr'] = 'orange' 
cp.process_item(item,"Yo") 
cp.process_item(item2,"Yo") 
cp.json() 
+0

真的真的很感謝你, 我會試試這個方法! – Amily

+0

我已經清理了一些代碼。如果它幫助您解決問題,請接受答案 –

0

而且我發現這個功能

class CategoryPipeline(object): 
    global inf_dict, ctgr 
    inf_dict = lambda: collections.defaultdict(inf_dict) 
    ctgr = inf_dict() 

    def __init__(self): 
     global file 
     file = open("test.json","w") 

    def process_item(self, item, spider): 
     ctgr[item['bigCtgr']][item['smaCtgr']][item['ssCtgr']][item['sCtgr']] = 'NULL' 


    def __del__(self): 
     b = json.dumps(ctgr, ensure_ascii=False).encode('utf-8') 
     file.write(b) 
     file.write('\n') 
     file.close()