【问题标题】:How to convert CSV file to Array?如何将 CSV 文件转换为数组?
【发布时间】:2016-01-12 22:41:18
【问题描述】:

例如,我有 csv 文件,然后导入到 PowerShell:

$animal = Import-Csv “animal.csv”
$animal

输出是:

动物
------
鸟
猫
狗

我想转换为数组值,即我想把这些“鸟”、“猫”、“狗”放在数组中,基本上是这样的:

$newanimals = "Bird","Cat","Dog"

我该怎么做?

【问题讨论】:

    标签: arrays csv powershell


    【解决方案1】:
    $newanimals = Import-Csv -Path "animal.csv" | Select -ExpandProperty Animal
    

    【讨论】:

    • 非常好的答案,+1
    【解决方案2】:

    又快又脏:

    Import-Csv ".\animal.csv" | Foreach-Object {$arrayOfAnimals += @($_.Animal)}
    Write-Host $arrayOfAnimals #Bird cat dog
    $arrayOfAnimals[1] #cat
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-01
      • 2012-08-25
      • 1970-01-01
      • 2018-07-11
      • 2012-12-21
      • 1970-01-01
      • 2020-06-08
      • 2020-05-24
      相关资源
      最近更新 更多