Export-Csv添加不需要的标头双引号

时间:2018-08-24 07:10:33

标签: powershell csv header export-csv

我有一个源CSV文件(不包含标题,所有列均以逗号分隔),我试图根据第一列中的值并将该列值用作输出文件名,将其拆分为单独的CSV文件

输入文件:

S00000009,2016,M04 01/07/2016,0.00,0.00,0.00,0.00,0.00,0.00,750.00,0.00,0.00
S00000009,2016,M05 01/08/2016,0.00,0.00,0.00,0.00,0.00,0.00,600.00,0.00,0.00
S00000009,2016,M06 01/09/2016,0.00,0.00,0.00,0.00,0.00,0.00,600.00,0.00,0.00
S00000010,2015,W28 05/10/2015,2275.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00
S00000010,2015,W41 04/01/2016,0.00,0.00,0.00,0.00,0.00,0.00,568.75,0.00,0.00
S00000010,2015,W42 11/01/2016,0.00,0.00,0.00,0.00,0.00,0.00,568.75,0.00,0.00
S00000012,2015,W10 01/06/2015,0.00,0.00,0.00,0.00,0.00,0.00,650.00,0.00,0.00
S00000012,2015,W11 08/06/2015,0.00,0.00,0.00,0.00,0.00,0.00,650.00,0.00,0.00
S00000012,2015,W12 15/06/2015,0.00,0.00,0.00,0.00,0.00,0.00,650.00,0.00,0.00

我的PowerShell脚本如下:

Import-Csv INPUT_FILE.csv -Header service_id,year,period,cash_exp,cash_inc,cash_def,act_exp,act_inc,act_def,comm_exp,comm_inc,comm_def |
    Group-Object -Property "service_id" | 
    Foreach-Object {
        $path = $_.Name + ".csv";
        $_.group | Export-Csv -Path $path -NoTypeInformation
    }

输出文件:

S00000009.csv

"service_id","year","period","cash_exp","cash_inc","cash_def","act_exp","act_inc","act_def","comm_exp","comm_inc","comm_def"
"S00000009","2016","M04 01/07/2016","0.00","0.00","0.00","0.00","0.00","0.00","750.00","0.00","0.00"
"S00000009","2016","M05 01/08/2016","0.00","0.00","0.00","0.00","0.00","0.00","600.00","0.00","0.00"
"S00000009","2016","M06 01/09/2016","0.00","0.00","0.00","0.00","0.00","0.00","600.00","0.00","0.00"

S00000010.csv

"service_id","year","period","cash_exp","cash_inc","cash_def","act_exp","act_inc","act_def","comm_exp","comm_inc","comm_def"
"S00000010","2015","W28 05/10/2015","2275.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00"
"S00000010","2015","W41 04/01/2016","0.00","0.00","0.00","0.00","0.00","0.00","568.75","0.00","0.00"
"S00000010","2015","W42 11/01/2016","0.00","0.00","0.00","0.00","0.00","0.00","568.75","0.00","0.00"

它将使用列1(service_id)中的头值生成新文件。 有两个问题。

  1. 输出CSV文件包含我不需要的标题行。
  2. 这些列用双引号括起来,我不需要。

3 个答案:

答案 0 :(得分:1)

首先,.csv文件需要标头和引号作为csv文件结构。但是,如果您不想要它们,则可以继续使用文本文件或...

$temp = Import-Csv INPUT_FILE.csv -Header service_id,year,period,cash_exp,cash_inc,cash_def,act_exp,act_inc,act_def,comm_exp,comm_inc,comm_def | Group-Object -Property "service_id" | 
        Foreach-Object {
        $path=$_.name+".csv"
        $temp0 = $_.group | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1
        $temp1 = $temp0.replace("""","")
        $temp1 > $path

        }

但是此输出不是“真实的” csv文件。 希望有帮助。

答案 1 :(得分:1)

对于您的特定情况,您可以使用更简单的方法。将输入文件读取为纯文本文件,通过分割第一个字段将行分组,然后将组写入以组命名的输出文件:

Get-Content 'INPUT_FILE.csv' |
    Group-Object { $_.Split(',')[0] } |
    ForEach-Object { $_.Group | Set-Content ($_.Name + '.csv') }

答案 2 :(得分:0)

另一种解决方案

  • 不使用命名标头,而仅使用数字(因为无论如何都不希望在输出中使用它们)
  • 避免使用不必要的临时文件。
  • 仅删除定界双引号的字段。

Import-Csv INPUT_FILE.csv -Header (1..12) | 
  Group-Object -Property "1" | Foreach-Object {
    ($_.Group | ConvertTo-Csv -NoType | Select-Object -Skip 1).Trim('"') -replace '","',',' | 
       Set-Content -Path ("{0}.csv" -f $_.Name) 
}