我需要将以下格式的平面文件转换为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}$
这是相当简单的使用两个步骤的方法:首先,为每一行构造一个节点。然后,将每个节点连接到其子节点。在
结果:
^{pr2}$编辑:可以使用
del
语句从叶节点中删除children
属性。在结果:
相关问题 更多 >
编程相关推荐