将平面制表符分隔的文件转换为Json嵌套结构

2024-09-29 01:30:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要将以下格式的平面文件转换为JSON格式。输入和输出如下所示。我遇到过这样一个问题:Create nested JSON from CSV文章,但是我有一个额外的信息/字段level,用于确定JSON输出中的嵌套结构。Pythonpandas确实有df.to_json,但无法找到以所需的输出格式写入的方法。任何帮助都将不胜感激。在

输入:

name    level   children    size
aaa 7   aaab    2952
aaa 7   aaac    251
aaa 7   aaad    222
aaab    8   xxx 45
aaab    8   xxy 29
aaab    8   xxz 28
aaab    8   xxa 4
aaac    8   ddd 7
aaac    8   xxt 4
aaac    8   xxu 1
aaac    8   xxv 1
ddd 9   ppp 4
ddd 9   qqq 2

输出:

^{pr2}$

Tags: 文件csvfrom信息json格式create文章
1条回答
网友
1楼 · 发布于 2024-09-29 01:30:08

这是相当简单的使用两个步骤的方法:首先,为每一行构造一个节点。然后,将每个节点连接到其子节点。在

with open("data.txt") as file:
    lines = file.read().split("\n")

#remove header line.
lines = lines[1:]

entries = {}

#create an entry for each child node.
for line in lines:
    name, level, child, size = line.split()
    entries[child] = {"name": child, "size": int(size), "children": []}

#we now have an entry for all nodes that are a child of another node.
#but not for the topmost parent node, so we'll make one for it now.
parents  = set(line.split()[0] for line in lines)
children = set(line.split()[2] for line in lines)
top_parent = (parents - children).pop()
#(just guess the size, since it isn't supplied in the file)
entries[top_parent] = {"name": top_parent, "size": 5000, "children": []}

#hook up each entry to its children
for line in lines:
    name, level, child, size = line.split()
    entries[name]["children"].append(entries[child])

#the nested structure is ready to use!
structure = entries[top_parent]

#display the beautiful result
import pprint
pprint.pprint(structure)

结果:

^{pr2}$

编辑:可以使用del语句从叶节点中删除children属性。在

#execute this after the "hook up each entry to its children" section.
#remove "children" from leaf nodes.
for entry in entries.itervalues():
    if not entry["children"]:
        del entry["children"]

结果:

{'children': [{'children': [{'name': 'xxx', 'size': 45},
                            {'name': 'xxy', 'size': 29},
                            {'name': 'xxz', 'size': 28},
                            {'name': 'xxa', 'size': 4}],
               'name': 'aaab',
               'size': 2952},
              {'children': [{'children': [{'name': 'ppp', 'size': 4},
                                          {'name': 'qqq', 'size': 2}],
                             'name': 'ddd',
                             'size': 7},
                            {'name': 'xxt', 'size': 4},
                            {'name': 'xxu', 'size': 1},
                            {'name': 'xxv', 'size': 1}],
               'name': 'aaac',
               'size': 251},
              {'name': 'aaad', 'size': 222}],
 'name': 'aaa',
 'size': 5000}

相关问题 更多 >