使用Powershell创建/填充csv文件 [英] Create/populate a csv file with Powershell

查看:265
本文介绍了使用Powershell创建/填充csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用powershell创建/填充csv文件时遇到了一些麻烦.我是Powershell的新手,所以我可能会缺少一些明显的东西,所以请放轻松.情况如下:

首先,我要创建一个数组(?)作为我的表

#Create output table with headers
$output = @()
$row = New-Object System.Object
$row | Add-Member -MemberType NoteProperty -Name "Example Header 1" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "Example Header 2" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "Example Header 3" -Value $null
$output += $row

我正在使用将其写入文件 $output | Export-Csv new.csv -NoTypeInformation

这似乎是用我想要的标题制作一个csv文件.如果有更好的方法可以进行此操作,请告诉我.下一步是我遇到问题的地方.现在,我需要以编程方式用数据填充表.导入现有的csv文件时,我可以像数组一样访问/修改表中的数据(即$output[rowIndex]."Header Name" = "new data").

因此,我尝试将数据添加到新创建的表中.我写了$ouput[0]."Example Header 1" = "Test Data".这可以按我预期的方式工作,并使用测试数据"用指定的标题填充列中的第一行.但是,我只能访问[0]. $output[1]等导致错误,因为我猜它们不存在.我再次尝试使用$output += $row添加更多行,但是它根本不起作用,并且会引起一些奇怪的错误(如果我写一行,它会写所有行,可能是因为它都是同一对象)./p>

所以基本上我的问题是,如何从头创建一个csv文件,向其中添加一些标题,然后开始写入所有(未知/可变数量)行?我相信有更好的方法可以做到这一点,但是就像我说的那样,我对Powershell非常陌生.理想情况下,我希望能够按索引(0、1,2等)访问行,但是我可以接受任何内容.


基本解决方案(改编自 Martin Brandl的答案)

这基本上是从一个csv文件读取数据,然后将其插入具有新指定标头的另一个csv文件中.

$csv = Import-Csv "MyCsv.csv"
$newCsv = @()
foreach($row in $csv) {
    $newCsv += [PSCustomObject]@{
        "New Column Header1" = $row."Original Column Header1"
        "New Column Header2" = $row."Original Column Header2"
    }
}

解决方案

输出:

"Example Header 1","Example Header 2","Example Header 3"
"a","b","c"
"a2","b2","c2"
"a3","b4","c5"

I am having a little trouble creating/populating a csv file with powershell. I am new to powershell, so I may be missing some obvious things, so please go easy on me. Here is the situation:

First I am creating an array(?) to act as my table

#Create output table with headers
$output = @()
$row = New-Object System.Object
$row | Add-Member -MemberType NoteProperty -Name "Example Header 1" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "Example Header 2" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "Example Header 3" -Value $null
$output += $row

I am writing it to a file using $output | Export-Csv new.csv -NoTypeInformation

This appears to make a csv file with the headers that I want. If there is a better way to do this, please let me know. The next step is where I am running into problems. I now need to programatically populate the table with data. When importing existing csv files, I am able to access/modify data in the table like an array (i.e. $output[rowIndex]."Header Name" = "new data").

So I tried to add data to my newly created table. I wrote $ouput[0]."Example Header 1" = "Test Data". This works as I expected and populates the first row in the column with the specified header with "Test Data". However, I can ONLY access [0]. $output[1] and so on cause errors because I guess they do not exist. I tried using $output += $row again to add more rows, but it does not work at all and causes some strange errors to happen (if I write to a row, it writes to all rows, probably because its all the same object).

So basically my question is, how can I create a csv file from scratch, add some headers to it, and then start writing to all the (unknown/variable number of) rows? I am sure there is a better way to do it, but like I said, I am very new to powershell. Ideally I would like to be able to access rows by index (0,1,2, etc) but I am open to whatever.


Basic solution (adapted from Martin Brandl's answer)

This basically reads data from one csv file, and inserts it into another with new specified headers.

$csv = Import-Csv "MyCsv.csv"
$newCsv = @()
foreach($row in $csv) {
    $newCsv += [PSCustomObject]@{
        "New Column Header1" = $row."Original Column Header1"
        "New Column Header2" = $row."Original Column Header2"
    }
}

解决方案

As Mathias mentioned, you shouldn't create the CSV first containg only the headers. Instead, populate your CSV with the actual rows you want and export it:

[PSCustomObject]@{
    'Example Header 1' = "a"
    'Example Header 2' = "b"
    'Example Header 3' = "c"
}, [PSCustomObject]@{
    'Example Header 1' = "a2"
    'Example Header 2' = "b2"
    'Example Header 3' = "c2"
}, [PSCustomObject]@{
    'Example Header 1' = "a3"
    'Example Header 2' = "b4"
    'Example Header 3' = "c5"
} | Export-Csv new.csv -NoTypeInformation

Output:

"Example Header 1","Example Header 2","Example Header 3"
"a","b","c"
"a2","b2","c2"
"a3","b4","c5"

这篇关于使用Powershell创建/填充csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆