在powershell中,使用export-csv cmdlet,是否有方法定义行终止符,而不是标准回车符?
我在csv文件中有包含回车的字段,比如电子邮件,如果可能的话,我想定义一个不同的行终止符,谢谢 似乎没有。查看带有Reflector的命令的代码,它使用TextWriter的WriteLine方法写入行,该方法在结尾使用…看起来不像。查看带有Reflector的命令的代码,该命令使用TextWriter的WriteLine方法写入行,该方法在结尾使用…根据惯例,csv字段中的多行字段由双引号(“)分隔。Powershell应该这样做,或者您:在powershell中,使用export-csv cmdlet,是否有方法定义行终止符,而不是标准回车符?,powershell,Powershell,我在csv文件中有包含回车的字段,比如电子邮件,如果可能的话,我想定义一个不同的行终止符,谢谢 似乎没有。查看带有Reflector的命令的代码,它使用TextWriter的WriteLine方法写入行,该方法在结尾使用…看起来不像。查看带有Reflector的命令的代码,该命令使用TextWriter的WriteLine方法写入行,该方法在结尾使用…根据惯例,csv字段中的多行字段由双引号(“)分隔。Powershell应该这样做,或者您: PS C:\> $ob = new-objec
PS C:\> $ob = new-object PSObject |
add-member -memberType NoteProperty -name "header" -value "header1" -PassThru |
add-member -memberType NoteProperty -name "body" -value @"
Body text
with carriage returns
"@ `
-PassThru |
add-member -memberType NoteProperty -name "tail" -value "tail1" -PassThru
PS C:\> $ob
header body tail
------ ---- ----
header1 Body text... tail1
PS C:\> $ob | Export-Csv \temp\ml.csv
PS C:\> get-content \temp\ml.csv
#TYPE System.Management.Automation.PSCustomObject
"header","body","tail"
"header1","Body text
with carriage returns","tail1"
现在,导入此文件时应提供原始文件:
PS C:\> (import-csv C:\Temp\ml.csv).body
Body text
with carriage returns
按照惯例,csv字段中的多行字段由双引号(“)分隔。Powershell应执行此操作,或者您:
PS C:\> $ob = new-object PSObject |
add-member -memberType NoteProperty -name "header" -value "header1" -PassThru |
add-member -memberType NoteProperty -name "body" -value @"
Body text
with carriage returns
"@ `
-PassThru |
add-member -memberType NoteProperty -name "tail" -value "tail1" -PassThru
PS C:\> $ob
header body tail
------ ---- ----
header1 Body text... tail1
PS C:\> $ob | Export-Csv \temp\ml.csv
PS C:\> get-content \temp\ml.csv
#TYPE System.Management.Automation.PSCustomObject
"header","body","tail"
"header1","Body text
with carriage returns","tail1"
现在,导入此文件时应提供原始文件:
PS C:\> (import-csv C:\Temp\ml.csv).body
Body text
with carriage returns
实际上,您可以使用-分隔符对行进行分类,例如我使用逗号“,”对行进行分类
import-csv "C:\Path" -Delimiter ","
实际上,您可以使用-分隔符对行进行分类,例如我使用逗号“,”对行进行分类
import-csv "C:\Path" -Delimiter ","
这里有一个解决方案:
function Remove-LineBreaks {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline=$true)]
[psobject]$InputObject #csv row
,
[Parameter()]
[string]$Replacement = ''
)
process {
$InputObject | %{$_ -replace "`n", $Replacement}
}
}
clear-host
$example = 1..3 | %{new-object -TypeName PSObject -Property @{
Index=$_
Name=("I'm No {0:00}" -f $_)
Text=@"
this is some multiline text
for item index $_
blah blah blah
"@
}}
write-host 'With Line Breaks' -ForegroundColor Green
$example | convertto-csv -NoTypeInformation
write-host 'Without Line Breaks' -ForegroundColor Green
$example | convertto-csv -NoTypeInformation | Remove-LineBreaks -Replacement '; '
然后,您可以将其转换回一个对象,该对象通过在末尾添加|ConvertFrom CSV
删除了其所有参数行,或者通过添加|set content-Path'.\filename.CSV'
将其输出到文件中。以下是一个解决方案:
function Remove-LineBreaks {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline=$true)]
[psobject]$InputObject #csv row
,
[Parameter()]
[string]$Replacement = ''
)
process {
$InputObject | %{$_ -replace "`n", $Replacement}
}
}
clear-host
$example = 1..3 | %{new-object -TypeName PSObject -Property @{
Index=$_
Name=("I'm No {0:00}" -f $_)
Text=@"
this is some multiline text
for item index $_
blah blah blah
"@
}}
write-host 'With Line Breaks' -ForegroundColor Green
$example | convertto-csv -NoTypeInformation
write-host 'Without Line Breaks' -ForegroundColor Green
$example | convertto-csv -NoTypeInformation | Remove-LineBreaks -Replacement '; '
然后,您可以将其转换回一个对象,该对象通过在末尾添加
|ConvertFrom CSV
删除了其所有参数行,或者通过添加|set content-Path'.\filename.CSV'
Aww,可以输出到文件,这是令人失望的…那么你对如何编辑csv或回避问题有什么建议吗?啊,这是令人失望的…那么你对如何编辑csv或回避问题有什么建议吗?最大的csv文件有多大?最大的csv文件有多大?