python 这种结构excel怎么生成json?
这种结构,怎么生成json呢
比如:
{
"游戏":{ "游戏(按玩法):{}
"游戏(按主题):xxx
}
难死我了····
回答:
# 方法 1from functools import reduce
s = ['a', 'a b', 'a b c', 'a b d', 'f', 'f c', 'f e']
def insert(obj, item):
reduce(lambda c, i: c[i] if i in c else c.setdefault(i, {}), item.split(), obj)
return obj
print(reduce(insert, s, {}))
# 方法2import json
class TrieTree:
def __init__(self):
self.root = {}
def insert(self, item):
current = self.root
for i in item.split():
if i in current:
current = current[i]
else:
current = current.setdefault(i, {})
def to_json(self):
return json.dumps(self.root, ensure_ascii=False)
tree = TrieTree()for item in s:
tree.insert(item)
print(tree.to_json())
{"a": {"b": {"c": {}, "d": {}}}, "f": {"c": {}, "e": {}}}
回答:
用pandas库读取pd.read_excel得到dataframe对象,然后做条件筛选组合,做成想要的格式,再to_json就搞定啦
以上是 python 这种结构excel怎么生成json? 的全部内容, 来源链接: utcz.com/a/161134.html