如何将CSV文件转换为数组?

时间:2016-01-12 22:41:18

标签: arrays csv powershell

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

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

输出是:

Animal
------
Bird
cat
dog

我想转换为数组值,即我想将这些“bird”,“cat”,“dog”放在数组中,基本上就是这样:

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

我该怎么做?

2 个答案:

答案 0 :(得分:3)

$newanimals = Import-Csv -Path "animal.csv" | Select -ExpandProperty Animal

答案 1 :(得分:1)

又快又脏:

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