我在csv文件中有字段,其中有回车符,想想电子邮件,我想尽可能定义一个不同的行终止符,谢谢!
答案 0 :(得分:2)
按照惯例,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
答案 1 :(得分:1)
看起来没有。使用Reflector查看命令的代码,它使用TextWriter的WriteLine方法写入行,该方法最后使用\ r \ n ...
答案 2 :(得分:1)
这是一个解决方案:
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'
输出到文件。
答案 3 :(得分:0)
实际上你可以使用-Delimiter对行进行分类,例如我使用逗号","对行进行分类..
import-csv "C:\Path" -Delimiter ","