【问题标题】:Powershell CSV Column populated from Array从数组填充的 Powershell CSV 列
【发布时间】:2019-05-23 00:05:45
【问题描述】:

假设我有一个具有这种格式的 csv:其中 "" 表示一个空值。

column1 column2 column3 column4
------- ------- ------- -------
123     555     aaa     ""
456     555     bbb     ""
789     666     ccc     ""

我们可以看到行的长度是 3。 我有一个包含 3 个项目的数组,我想用它们填充第 4 列。

$arr123 = @("iii","jjj","kkk")

如何使用Add-Member 将数组值添加到行中,这样结果会是这样的?

column1 column2 column3 column4
------- ------- ------- -------
123     555     aaa     "iii"
456     555     bbb     "jjj"
789     666     ccc     "kkk"

我已经尝试了一些东西,但无法得到它。我尝试过的一件我认为可行的方法是删除 column4,然后尝试将其添加回来。

for($i=4; $i -lt $myCSVvariable.Count; $i++) {
$myCSVvariable | Add-Member -MemberType NoteProperty -Name "column4" -Value "$arr123[$i]" -Force
 }

我真的觉得 for 循环应该做到了,我做错了什么?我还有其他方法可以采取吗?

【问题讨论】:

  • 你如何决定什么数组项与什么 CSV 行/对象一起使用?
  • 它只是一个数组,没有可以添加的属性/成员。 += 将项目添加到数组中(通过创建新数组)。

标签: powershell csv


【解决方案1】:

我完全同意Drew 的观点,即您应该以某种方式确保您插入的数据与每一行中的其余数据一致。

根据您的问题,我可以清楚地看到您的示例 CSV 中存在第四列。但是,您也是在说“我们可以看到行的长度是 3”。这与您向我们展示的内容不符..

场景 1
CSV 文件确实已经有带有标题的第四列,看起来像

"column1","column2","column3","column4"
"123","555","aaa",""
"456","555","bbb",""
"789","666","ccc",""
"321","777","ddd",""

那么这应该做你想要的:

$csv    = Import-Csv 'D:\yourdata.csv'
$arr123 = "iii","jjj","kkk"

# figure out the minimum lenght for the loop, so we don't run into 
# error: "Index was outside bounds of array"
$numItems = [math]::Min($csv.Count, $arr123.Count)

# now loop through the rows and insert the data by index
for ($i = 0; $i -lt $numItems; $i++) {
    $csv[$i].column4 = $arr123[$i]
}

# show the result on screen
$csv | Format-Table -AutoSize

# or export to a new CSV file
# $csv | Export-Csv -Path 'D:\yourdata_Extended.csv' -NoTypeInformation -Force


场景 2
但是,如果您的 CSV 文件确实 NO 已经有第四列并且看起来像
"column1","column2","column3"
"123","555","aaa"
"456","555","bbb"
"789","666","ccc"
"321","777","ddd"

然后,您需要为每一行添加一列并插入数组中的数据:

$csv    = Import-Csv 'D:\yourdata.csv'
$arr123 = "iii","jjj","kkk"

# now loop through all the CSV rows and insert a new column and array data (if available)
for ($i = 0; $i -lt $csv.Count; $i++) {
    $value = if ($i -lt $arr123.Count) { $arr123[$i] } else { $null }
    $csv[$i] | Add-Member -MemberType NoteProperty -Name "column4" -Value $value
}

# show the result on screen
$csv | Format-Table -AutoSize

# or export to a new CSV file
# $csv | Export-Csv -Path 'D:\yourdata_Extended.csv' -NoTypeInformation -Force

这两种情况都会导致更新的 csv(如控制台上使用 Format-Table -AutoSize 所示):

column1 column2 column3 column4
------- ------- ------- -------
123     555     aaa     iii    
456     555     bbb     jjj    
789     666     ccc     kkk    
321     777     ddd

附注对于数组 $arr123 的声明,您不需要将元素的值放在 @() 构造中

【讨论】:

    【解决方案2】:

    这是一种稍微不同的方法,因为它循环遍历 CSV 而不是数组。

    这可能不是最好的方法。我建议查看 Hashtable 以确保您添加到 CSV 的数据与另一列数据匹配。

    # Import the CSV
    $csv = import-csv C:\folder\MyCSV.csv
    
    # Establish the Array
    $arr123 = @("iii","jjj","kkk")
    # Set counter for array iteration
    $i = 0
    
    # Loop through each row in CSV
    Foreach($Row in $csv) {
        # Add array item based on row of CSV, assuming each row relates to the corresponding index of array
        $Row | Add-Member -MemberType NoteProperty -Name "column4" -Value $arr123[$i] -Force
        # Increase counter
        $i++
    }
    
    # Export modifications
    $csv | Export-Csv C:\folder\MyCSV.csv -NoTypeInformation
    

    【讨论】:

      猜你喜欢
      • 2021-09-27
      • 2019-08-29
      • 2019-09-29
      • 1970-01-01
      • 2021-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      相关资源
      最近更新 更多