1 回答
TA贡献1827条经验 获得超4个赞
您可以使用itertools.groupby递归:
import itertools as it, re
data = [[*re.findall('^\s+', b), *re.split('(?<=[A-Z])\s+', i)] for b in open('os_stuff.txt') if not (i:=re.sub('^\s+|\sStart\n$', '', b)).endswith('End\n')]
def to_tree(d):
_d = [(a, list(b)) for a, b in it.groupby(d, key=lambda x:bool(re.findall('^\s+$', x[0])))]
new_dict, _last = {}, None
for i, [a, b] in enumerate(_d):
if not a:
for j, *k in b:
if not k or (not k[0] and i < len(_d) - 2):
_last = j
else:
new_dict[j] = ' '.join(k).strip('\n')
else:
new_dict[_last] = [to_tree([[k[2:], *j] if k[2:] else j for k, *j in b])]
return new_dict
import json
print(json.dumps(to_tree(data), indent=4))
输出:
{
"Hostinfo": [
{
"DATE": "190819 1522",
"HOST": "midas",
"DOMAIN": "test.de",
"HW_PLATFORM": "x86_64",
"SERVER_TYPE": "virtual",
"CPU_INFO": [
{
"CPU_TYPE": "Intel(R) Xeon(R) CPU E7-8867 v4 @ 2.40GHz",
"CPU_COUNT": "2",
"CORE_COUNT": "2"
}
],
"THREAD_COUNT": "8",
"MEMORY": "32951312 kB ",
"OS": [
{
"OS": "Linux",
"OS_VERSION": "4.9.0-6-amd64",
"OS_UPTIME": "536 days 21:08"
}
],
"RELEASE": "Debian GNU/Linux 9 (stretch)",
"RELEASE_VERSION": "9",
"RELEASE_PATCHLEVEL": ""
}
]
}
编辑:Python2.7 解决方案:
import itertools as it, re
new_data = [[i, re.sub('^\s+|\sStart\n$', '', i)] for i in open('os_stuff.txt')]
data = [re.findall('^\s+', a)+re.split('(?<=[A-Z])\s+', b) for a, b in new_data if not b.endswith('End\n')]
def to_tree(d):
_d = [(a, list(b)) for a, b in it.groupby(d, key=lambda x:bool(re.findall('^\s+$', x[0])))]
new_dict, _last = {}, None
for i, [a, b] in enumerate(_d):
if not a:
for j_k in b:
if not j_k[1:] or (not j_k[1:][0] and i < len(_d) - 2):
_last = j_k[0]
else:
new_dict[j_k[0]] = ' '.join(j_k[1:]).strip('\n')
else:
new_dict[_last] = [to_tree([[k_j[0][2:]]+k_j[1:] if k_j[0][2:] else k_j[1:] for k_j in b])]
return new_dict
print(to_dict(data))
添加回答
举报