Advice for multi level csv file creation

萝らか妹 提交于 2019-12-24 17:43:35

问题


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

While a child node might contain:

Name, ChildId

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.

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>

回答1:


The XML could be minimized to this:

<names>
 <name1>
  <childName1/>
  <childName2/>
 </name1>

 <name2>
  <childName1/>
  <childName2/>
 </name2>
</names>

And the CSV to this:

name1  ChildName1
name1  ChildName2
name2  ChildName1
name2  ChildName2

with a JSON serialization like this:

{"names":
 [
  {
  "name1":
   [
    {
    "childName1":""
    },
    {
    "childName2":""
    }
   ]
  },
  {
  "name2":
   [
    {
    "childName1":""
    },
    {
    "childName2":""
    }
   ]
  }
 ]
}

In a row-oriented fashion, the XML could be:

<names>
 <name1 name="ChildName1|ChildName2">
 <name2 name="ChildName1|ChildName2">
</names>

And the corresponding CSV:

name1  ChildName1|ChildName2
name2  ChildName1|ChildName2

And the corresponding JSON:

{"names": [{"name1":"ChildName1|ChildName2"},{"name2":"ChildName1|ChildName2"}]}

References

  • Subelement Contents Versus Tag Attributes
  • Creating a Comma-Delimited File (CSV) with COBOL
  • EXI for JSON (EXI4JSON)
  • Process EXI for JSON
  • Schema and Table Semantics


来源:https://stackoverflow.com/questions/12710122/advice-for-multi-level-csv-file-creation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!