我有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 :(得分: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()
答案 1 :(得分: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()