将CSV内容编码为UTF-8

时间:2016-12-30 08:07:08

标签: powershell encoding utf-8

所以我有这个powershell脚本导入一个csv文件,将null替换为'0'并导出这个csv。

问题是这个csv的内容和标题是希伯来语

我几乎尝试了一切 使用-Encoding用于所有类型的编码,但没有

任何建议?

$propertyTranslation = @(
    @{ Name = 'Customer__c';   Expression = { $_.'לקוח' } }
    @{ Name = 'Name__c';       Expression = { $_.'שם'  } }
    @{ Name = 'CheckCount__c'; Expression = { $_.'כמות'  } }
    @{ Name = 'Deal';          Expression = { $_.'עסקהוזה'  } }
    @{ Name = 'Amount__c';     Expression = { $_.'סכום'  } }
    @{ Name = 'Discount__c';   Expression = { $_.'ניסיון'  } }
    # And so on
)

$csv = Import-Csv C:\Users\alon\Documents\again.csv -Header "Customer__c","Name__c","Deal","Amount__c","CheckCount__c","Discount__c" 

$csv | ForEach-Object {
    if($_.Customer__c -eq "")   { $_.Customer__c = "0" }
    if($_.Name__c -eq "")       { $_.Name__c = "0" }
    if($_.Deal -eq "")          { $_.Deal = "0" }
    if($_.Amount__c -eq "")     { $_.Amount__c = "0" }
    if($_.Discount__c -eq "")   { $_.Discount__c = "0" }
    if($_.CheckCount__c -eq "") { $_.CheckCount__c = "0" }
} 

Select-Object -Property $propertyTranslation 

$csv | Export-Csv C:\Users\alon\Documents\CheckDealBeforeUpsert.csv -NoTypeInformation -Encoding UTF8

1 个答案:

答案 0 :(得分:0)

Windows中使用的术语“ANSI”基本上是许多编码(或代码页)的总称。通常它指的是windows-1252编码。但是,您的输入文件似乎是使用windows-1255代码页进行编码的。

我不确定在PowerShell -Encoding ASCII中是否始终表示Windows-1252编码,或者是否针对本地化Windows版本进行了调整。如果没有调整,您可能需要将输入文件转换为编码Import-Csv才能处理,然后才能导入和修改数据:

$inFile  = 'C:\path\to\input.csv'
$outFile = 'C:\path\to\input_utf8.csv'

$reader = New-Object IO.StreamReader ($inFile, [Text.Encoding]::GetEncoding(1255))
$writer = New-Object IO.StreamWriter ($outFile, $false, [Text.Encoding]::UTF8)

while ($reader.Peek() -ge 0) {
  $writer.WriteLine($reader.ReadLine())
}

$reader.Close(); $reader.Dispose()
$writer.Close(); $writer.Dispose()