1

我有一个源 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) 中的标题值生成新文件。有2个问题。

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

3 回答 3

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 文件。希望有帮助。

于 2018-08-24T08:07:09.827 回答
1

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

Get-Content 'INPUT_FILE.csv' |
    Group-Object { $_.Split(',')[0] } |
    ForEach-Object { $_.Group | Set-Content ($_.Name + '.csv') }
于 2018-08-24T08:33:19.377 回答
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) 
}
于 2018-08-24T16:42:34.480 回答