ConvertTo-Csv输出没有引号

Moh*_*eem 14 powershell

ConvertTo-Csv用来得到逗号分隔输出

 get-process | convertto-csv -NoTypeInformation -Delimiter ","
Run Code Online (Sandbox Code Playgroud)

输出如下:

"__NounName","Name","Handles","VM","WS",".....
Run Code Online (Sandbox Code Playgroud)

但是我想得到没有引号的输出,比如

__NounName,Name,Handles,VM,WS....
Run Code Online (Sandbox Code Playgroud)

js2*_*010 15

嗯,我的 Mac 上有 Powershell 7 preview 1,Export-Csv 有一个 -UseQuotes 选项,您可以将其设置为从不。:)


JPB*_*anc 13

这是一种删除引号的方法

get-process | convertto-csv -NoTypeInformation -Delimiter "," | % {$_ -replace '"',''} 
Run Code Online (Sandbox Code Playgroud)

但它有一个严重的缺点,如果其中一个项目包含"它将被删除!

  • 这可能不适用于“get-process”的输出,但此解决方案还将对其中包含特殊字符的字段进行转义。(例如“aaa”、“bbb”、“cc、cc”、“ddd”)。 (2认同)
  • @SteveGuidi 您说得对,答案中写有。 (2认同)

小智 10

我今天在一张桌子上工作并在我在记事本中预览 CSV 文件时考虑了这个问题,并决定看看其他人提出了什么。似乎很多人都使解决方案过于复杂。
这是从 PowerShell 中的 Export-Csv cmdlet 生成的 CSV 文件中删除引号的一种非常简单的方法。

使用以下数据创建 TEST.csv 文件。

“ID”、“姓名”、“州”
“5”、“斯蒂芬妮”、“亚利桑那”
“4”、“梅兰妮”、“俄勒冈”
“2”、“凯蒂”、“德克萨斯”
“8”、“史蒂夫” ","爱达荷州"
"9","多莉","田纳西州"

另存为:TEST.csv

将文件内容存储在 $Test 变量中
$Test = Get-Content .\TEST.csv

加载 $Test 变量以查看 get-content cmdlet 的结果
$Test

再次加载 $Test 变量并用逗号替换所有 ( "," ),然后通过删除每个引号来修剪开始和结束

$Test.Replace('","',",").TrimStart('"').TrimEnd('"')
Run Code Online (Sandbox Code Playgroud)

保存/替换 TEST.csv 文件

$Test.Replace('","',",").TrimStart('"').TrimEnd('"') | Out-File .\TEST.csv -Force -Confirm:$false
Run Code Online (Sandbox Code Playgroud)

使用 Import-Csv 和 Get-Content 测试新文件输出:

Import-Csv .\TEST.csv
Get-Content .\TEST.csv
Run Code Online (Sandbox Code Playgroud)

总而言之,这项工作可以用 2 行代码完成

$Test = Get-Content .\TEST.csv
$Test.Replace('","',",").TrimStart('"').TrimEnd('"') | Out-File .\TEST.csv -Force -Confirm:$false
Run Code Online (Sandbox Code Playgroud)


bwe*_*rks 7

这与接受的答案非常相似,但它有助于防止不必要地删除“真实”引号。

$delimiter = ','
Get-Process | ConvertTo-Csv -Delimiter $delimiter -NoTypeInformation | foreach { $_ -replace '^"','' -replace "`"$delimiter`"",$delimiter -replace '"$','' }
Run Code Online (Sandbox Code Playgroud)

这将执行以下操作:

  • 删除开始一行的引号
  • 删除结束一行的引号
  • 用单独的分隔符替换包含分隔符的引号。

因此,唯一会出错的方法是,其中一个值实际上不仅包含引号,而且特别包含引号-分隔符-引号序列,希望这应该是非常罕见的。


Kor*_*ill 7

我遇到了这个问题,发现了这个问题,但对答案并不满意,因为如果您使用的数据包含一个应该保持引用的分隔符,它们似乎都会受到影响。摆脱不需要的双引号是一件好事。

下面的解决方案似乎可以解决一般情况下的这个问题,以及所有会导致问题的变体。

我在别处找到了这个答案,从 PowerShell 创建的 CSV 中删除引号,并使用它为 SO 社区编写示例答案。

归因: 正则表达式的功劳,100% 归功于 Russ Loski。

函数中的代码,Remove-DoubleQuotesFromCsv

function Remove-DoubleQuotesFromCsv
{
    param (
        [Parameter(Mandatory=$true)]
        [string]
        $InputFile,

        [string]
        $OutputFile
    )

    if (-not $OutputFile)
    {
        $OutputFile = $InputFile
    }

    $inputCsv = Import-Csv $InputFile

    $quotedData = $inputCsv | ConvertTo-Csv -NoTypeInformation

    $outputCsv = $quotedData | % {$_ -replace  `
        '\G(?<start>^|,)(("(?<output>[^,"]*?)"(?=,|$))|(?<output>".*?(?<!")("")*?"(?=,|$)))' `
        ,'${start}${output}'}

    $outputCsv | Out-File $OutputFile -Encoding utf8 -Force
}
Run Code Online (Sandbox Code Playgroud)

测试代码

$csvData = @"
id,string,notes,number
1,hello world.,classic,123
2,"a comma, is in here","test data 1",345
3,",a comma, is in here","test data 2",346
4,"a comma, is in here,","test data 3",347
5,"a comma, is in here,","test data 4`r`nwith a newline",347
6,hello world2.,classic,123
"@

$data = $csvData | ConvertFrom-Csv
"`r`n---- data ---"
$data

$quotedData = $data | ConvertTo-Csv -NoTypeInformation
"`r`n---- quotedData ---"
$quotedData

# this regular expression comes from:
# http://www.sqlmovers.com/removing-quotes-from-csv-created-by-powershell/
$fixedData = $quotedData | % {$_ -replace  `
  '\G(?<start>^|,)(("(?<output>[^,"\n]*?)"(?=,|$))|(?<output>".*?(?<!")("")*?"(?=,|$)))' `
  ,'${start}${output}'}
"`r`n---- fixedData ---"
$fixedData

$fixedData | Out-File e:\test.csv -Encoding ascii -Force
"`r`n---- e:\test.csv ---"
Get-Content e:\test.csv
Run Code Online (Sandbox Code Playgroud)

测试输出

---- data ---

id string               notes                       number
-- ------               -----                       ------
1  hello world.         classic                     123   
2  a comma, is in here  test data 1                 345   
3  ,a comma, is in here test data 2                 346   
4  a comma, is in here, test data 3                 347   
5  a comma, is in here, test data 4...              347   
6  hello world2.        classic                     123   

---- quotedData ---
"id","string","notes","number"
"1","hello world.","classic","123"
"2","a comma, is in here","test data 1","345"
"3",",a comma, is in here","test data 2","346"
"4","a comma, is in here,","test data 3","347"
"5","a comma, is in here,","test data 4
with a newline","347"
"6","hello world2.","classic","123"

---- fixedData ---
id,string,notes,number
1,hello world.,classic,123
2,"a comma, is in here",test data 1,345
3,",a comma, is in here",test data 2,346
4,"a comma, is in here,",test data 3,347
5,"a comma, is in here,","test data 4
with a newline","347"
6,hello world2.,classic,123

---- e:\test.csv ---
id,string,notes,number
1,hello world.,classic,123
2,"a comma, is in here",test data 1,345
3,",a comma, is in here",test data 2,346
4,"a comma, is in here,",test data 3,347
5,"a comma, is in here,","test data 4
with a newline","347"
6,hello world2.,classic,123
Run Code Online (Sandbox Code Playgroud)


Tia*_*ica 6

生成文件后,您可以运行

set-content FILENAME.csv ((get-content FILENAME.csv) -replace '"')
Run Code Online (Sandbox Code Playgroud)

  • 如果我有一个包含三个参数的 CSV 行,例如“123”、“Sanchez、Rick”、“Scientist”,这会将其变成四个参数。后续的输入过程将无法正确导入。 (2认同)