将csv文件转换为json,其中一个字段可以转换为数组

时间:2016-10-21 07:19:45

标签: arrays json csv

目前我正在努力将一些数据放在一起作为csv文件,目的是将其转换为json对象。例如,这就是我的数据行的几行(第一行是带有每列标题的标题)

    Scientific Name     | Threats                      | Range States
    Psittacula eupatria | illegal trade, habitat loss  | Afghanistan, Bangladesh, Bhutan
    Cacatua alba        | illegal trade, overharvesting|Indonesia

我想知道哪个领域有多个与之相关的元素(例如,威胁的第一行有非法贸易,栖息地丧失,而范围国家则有阿富汗,孟加拉国,不丹)在那里在将csv文件转换为json时将这些存储到数组的方法?

所以json会是这样的吗?

[  
   {"Scientific Name": "Psittacula eupatria",
     "Threats":["illegal trade", "habitat loss"],
     "Range States":["Afghanistan","Bangladesh","Bhutan"]
   },
   {"Scientific Name": "Cacatua alba",
     "Threats":["illegal trade", "overharvesting"],
     "Range States":["Indonesia"]
   }
]

2 个答案:

答案 0 :(得分:1)

当然。

将代码中的整个字符串"illegal trade, habitat loss"作为变量后,您只需将其拆分为逗号(包括空格)即可。您是如何做到这一点的基础是您正在使用的语言/框架。

使用Javascript:

let csvCell = { ... };
let cellArray = csvCell.split(", ");
// cellArray contains ["illegal trade", "habitat loss"]

Python / Ruby:

csv_cell = { ... }
cell_array = csv_cell.split(", ")
# cell_array contains ["illegal trade", "habitat loss"]

访问:

import "strings"

var csvCell string = { ... }
var cellArray = strings.Split(csvCell, ", ")

等。等

答案 1 :(得分:0)

以下是使用jq

的解决方案

如果文件filter.jq包含

[
  split("\n")                             # split string into lines
| [.[0]    | split("|")[]] as $headers    # split header
| (.[1:][] | split("|"))                  # split data
| select(length>0)                        # eliminate blanks
| [   [ $headers, . ]                     # 
    | transpose[]                         # assemble objects
    | {key:.[0], value:.[1]|split(", ")}  # from keys and values
  ] | from_entries                        #
]

data包含

Scientific Name|Threats|Range States
Psittacula eupatria|illegal trade, habitat loss|Afghanistan, Bangladesh, Bhutan
Cacatua alba|illegal trade, overharvesting|Indonesia

然后命令

jq -M -R -s -r -f filter.jq data 

将产生输出

[
  {
    "Scientific Name": [
      "Psittacula eupatria"
    ],
    "Threats": [
      "illegal trade",
      "habitat loss"
    ],
    "Range States": [
      "Afghanistan",
      "Bangladesh",
      "Bhutan"
    ]
  },
  {
    "Scientific Name": [
      "Cacatua alba"
    ],
    "Threats": [
      "illegal trade",
      "overharvesting"
    ],
    "Range States": [
      "Indonesia"
    ]
  }
]