【问题标题】:convert a csv file to json where one of the fields can be converted into an array将 csv 文件转换为 json,其中一个字段可以转换为数组
【发布时间】:2016-10-21 07:19:45
【问题描述】:

目前我正在将一些数据作为 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"]
   }
]

【问题讨论】:

  • 你测试过什么吗?如果有,能否给我们看一下相应的代码?
  • 不,我还没有编写脚本来这样做,我只是想看看是否有一个简单的替代方案。 Javascript还是python?

标签: arrays json csv


【解决方案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, ", ")

等等。等等

【讨论】:

    【解决方案2】:

    这是使用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"
        ]
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-10
      • 2016-01-28
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多