尝试基于此示例从csv创建4个深层嵌套JSON:
Region,Company,Department,Expense,Cost
Gondwanaland,Bobs Bits,Operations,nuts,332
Gondwanaland,Bobs Bits,Operations,bolts,254
Gondwanaland,Maureens Melons,Operations,nuts,123
Run Code Online (Sandbox Code Playgroud)
在每个级别,我想总结成本并将其包含在相关级别的输出JSON中.
输出的JSON的结构应如下所示:
{
"id": "aUniqueIdentifier",
"name": "usually a nodes name",
"data": [
{
"key": "some key",
"value": "some value"
},
{
"key": "some other key",
"value": "some other value"
}
],
"children": [/* other nodes or empty */ ]
}
Run Code Online (Sandbox Code Playgroud)
(参考:http://blog.thejit.org/2008/04/27/feeding-json-tree-structures-to-the-jit/)
按照python中递归函数的思路思考但到目前为止这种方法还没有取得多大成功...对快速简便解决方案的任何建议都非常感激?
更新:逐渐放弃总结成本的想法,因为我无法弄明白:(.我还不是一个python编码器)!只需能够生成格式化的JSON就足够了,如果必须的话,我可以稍后插入数字.
一直在阅读,谷歌搜索和阅读解决方案,并在途中学到了很多,但仍然没有成功从上面的CSV结构创建我的嵌套JSON文件.网上某处必须是一个简单的解决方案吗?也许其他人的搜索条件更幸运了????