Powershell将引号添加到拆分的CSV文件中

时间:2019-06-08 14:37:41

标签: powershell csv

我正在尝试按经度列的前几位拆分一个csv文件。这是一个示例:

X,Y,TYPE,SPEED,DirType,Direction
-44.058251,-19.945982,1,30,1,339
-54.629503,-20.497509,1,30,1,263
-54.646202,-20.496151,1,30,1,86

我不了解powershell,但是我在网上找到了一些脚本,它可以满足我的要求:

Import-Csv maparadar.csv 
| Group-Object -Property {($_.x)[0..2] -join ""} 
| Foreach-Object {$path=$_.name+".csv" ; $_.group 
| Export-Csv -Path $path -NoTypeInformation}

这样我得到-44.csv,-54.csv之类的输出文件 但这会在输出文件的每个字段中添加不必要的引号,例如:

"X","Y","TYPE","SPEED","DirType","Direction"
"-46.521991","-23.690235","1","30","1","169"
"-46.670774","-23.756021","1","30","1","281"
"-46.549897","-23.120720","1","30","1","99"

有什么办法可以在不添加引号的情况下导出csv?

1 个答案:

答案 0 :(得分:3)

以下内容应提供所需的输出:

Import-Csv maparadar.csv | 
    Group-Object -Property {($_.x)[0..2] -join ""} | 
      Foreach-Object { $path=$_.name+".csv" ; ($_.group |
        ConvertTo-Csv -NoTypeInformation) -Replace '"' |
          Set-Content -Path $path }

说明:

我们用Export-Csv替换了您的ConvertTo-Csv,这将CSV输出提供到控制台/管道,而不是输出到文件。这些CSV格式的输出通过-Replace运算符发送,以替换文字"字符。最后,使用Set-Content -Path $path将格式化的输出发送到所需的文件。