附加到 CSV 文件

问题描述 投票:0回答:1

我正在尝试创建一个处理多个 csv 文件的程序。我创建了一个输出文件,其中第一个 csv 文件添加了标题。现在我正在尝试将下一个文件附加到其中。

这是 $AFw 文件的第一行:

"08-01-2023 09:00:00","08-02-2023 09:00:00","1","DAYTONA","104.5","1045","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""

我有以下代码来执行附加操作:

    $Append = Import-Csv $AF
    $AppendNew = Import-Csv $AFw 

    # Append contents of source CSV to destination CSV
    $Append1 = $Append + $AppendNew

    # Export the updated data to the destination CSV file
    $Append1 | Export-Csv -Path $AF -NoTypeInformation

输入文件需要许多空白列 (87),然后是最后一个字段。 第一个文件的最后一行以及附加文件的第一行和第二行如下。

"12/31/2023 09:00:00","01-01-2024 00:00:00","BRANDY BRANCH","9990000","26,138.0","0","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","10.451000"
,,,,,,"1",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,"1",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

当我通过调试器运行程序时,在附加它之前,它在 AppendNew 变量中似乎是正确的。感谢您的帮助。

powershell csv append
1个回答
0
投票

Import-Csv
将文件的第一行作为标题行,除非指定了
-Header

此外,使用

List
可能会更有效

试试这个:

$CsvCollection = [System.Collections.Generic.List[object]]::new()
$CsvCollection.add((Import-Csv $AF -Header the, fields, you, need <#skip -Header if there a header in the file #> ))
$CsvCollection.add((Import-Csv $AFw -Header the, fields, you, need <#skip -Header if there a header in the file #> ))
$CsvCollection | Export-Csv -Path $AF -NoTypeInformation
© www.soinside.com 2019 - 2024. All rights reserved.