建议多级csv文件创建 [英] Advice for multi level csv file creation
问题描述
我有一些最终用户想要的数据在CSV文件中。每个条目都有一个父节点和零个或多个子节点。例如,父节点可能包含:
I have some data that end user wants in a CSV file. Each entry has a parent node and zero or more child nodes. For example, a parent node might contain:
Name, Id, Date
子节点可能包含:
Name, ChildId
因此,我要搜索的是一个用CSV表示多级数据的标准。在XML中,我可以轻松地创建子节点。在CSV中最好的方法是什么?我想创建一个脚本来提取这些数据,而不会对什么是父数据和什么是子数据产生任何混淆。
So what I am searching for is a standard to represent multi-level data in CSV. In XML, I can easily create sub nodes. What is the best way to do this in CSV? I want to create a script to extract this data without any confusion about what is parent data and what is child data.
在XML中,它可能如下所示:
In XML this might look like:
<Parent Name="name1">
<Child Name="ChildName1"></Child>
<Child Name="ChildName2"></Child>
</Parent>
<Parent Name="name2">
<Child Name="ChildName1"></Child>
<Child Name="ChildName2"></Child>
</Parent>
推荐答案
XML可以最小化为:
The XML could be minimized to this:
<names>
<name1>
<childName1/>
<childName2/>
</name1>
<name2>
<childName1/>
<childName2/>
</name2>
</names>
以及CSV到此:
name1 ChildName1
name1 ChildName2
name2 ChildName1
name2 ChildName2
这样的JSON序列化:
with a JSON serialization like this:
{"names":
[
{
"name1":
[
{
"childName1":""
},
{
"childName2":""
}
]
},
{
"name2":
[
{
"childName1":""
},
{
"childName2":""
}
]
}
]
}
在面向行的方式中,XML可以是:
In a row-oriented fashion, the XML could be:
<names>
<name1 name="ChildName1|ChildName2">
<name2 name="ChildName1|ChildName2">
</names>
以及相应的CSV:
name1 ChildName1|ChildName2
name2 ChildName1|ChildName2
而相应的JSON:
{"names": [{"name1":"ChildName1|ChildName2"},{"name2":"ChildName1|ChildName2"}]}
参考
- Subelement Contents Versus Tag Attributes
- Creating a Comma-Delimited File (CSV) with COBOL
- EXI for JSON (EXI4JSON)
- Process EXI for JSON
这篇关于建议多级csv文件创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!