将双引号添加到所有用逗号分隔的列中,而不要添加已用双引号引起来的逗号

时间:2020-08-04 04:59:35

标签: powershell csv

我尝试过的是

(import-csv C:\file.csv) | export-csv C:\file.csv -NoTypeInformation -Encoding UTF8

上面的代码可以工作,但是即使文件大小小于100kb也要花费一些时间。(大约220行和50列)

示例:

csv中的内容

row 1 - textOne,"text with a,comma",,sample

row 2 - textTwo, demo text ,,stack

输出应为

row 1 - "textOne","text with a,comma","","sample"

row 2 - "textTwo","demo text","","stack"

1 个答案:

答案 0 :(得分:0)

通过使用Microsoft.VisualBasic.FileIO.TextFieldParser导入csv而不是使用Import-Csv cmdlet,您可以加快此过程。

尝试一下是否确实更快:

Add-Type -AssemblyName 'Microsoft.VisualBasic'

# you'll need to use the full absolute path to the file
$file   = "C:\file.csv"

# older PowerShell versions use:
# $parser = New-Object Microsoft.VisualBasic.FileIO.TextFieldParser $file
$parser = [Microsoft.VisualBasic.FileIO.TextFieldParser]::new($file)
$parser.SetDelimiters(',')
$header = $parser.ReadFields()

$csv = while (!$parser.EndOfData) {
    $i = 0
    $row = [ordered]@{}
    foreach ($field in $parser.ReadFields()) {
        $row[$header[$i++]] = $field
    }
    [PSCustomObject]$row
}
# clean up
$parser.Dispose()

# export the csv in UTF8
$csv | Export-Csv -Path $file -NoTypeInformation -Encoding UTF8