Alon Segal
Alon Segal

Reputation: 59

Encoding CSV content to UTF-8

So I have this powershell script Which import a csv file, replacing null into '0' and export this csv.

The issue is that the content and header of this csv is in hebrew

I tried almost everything Used -Encoding for all types of encoding but nothing

Any Suggestions?

$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

Upvotes: 1

Views: 3959

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200473

The term "ANSI" as it is used in Windows is basically an umbrella term for a number of encodings (or code pages). Usually it refers to the windows-1252 encoding. Your input file, however, appears to be encoded using the windows-1255 code page.

I'm not sure if in PowerShell -Encoding ASCII always means windows-1252 encoding, or if that is adjusted for localized Windows versions. If it's not adjusted you probably need to convert your input file to an encoding Import-Csv can handle before you can import and modify the data:

$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()

Upvotes: 2

Related Questions