Import-CSV / Export-CSV with special characters / Bug?

rated by 0 users
Not Answered This post has 0 verified answers | 7 Replies | 4 Followers

Top 25 Contributor
209 Posts
Microsoft MVP
Top Contributor
Tobias Weltner posted on 12-10-2008 8:55 AM

Is there a way to make import/export-csv work with special characters? Try this:

cd $home
md öttö
dir | select-object Name | export-csv test.csv
import-csv test.csv

All special characters appear as "?".  

-Tobias

All Replies

Top 50 Contributor
98 Posts
Top Contributor

Hi Tobias,

I have try this on german Windows Vista and it worked with german "Umlauten"

dir | select-object Name | export-csv test.csv -Encoding unicode
import-csv test.csv

Name
----
öttö
test.csv

Best regards
Martin

Top 25 Contributor
209 Posts
Microsoft MVP
Top Contributor

Thanks, that's it. You can control Unicode encoding when you use Export-CSV. The problem occured with csv files in ANSI format generated by Excel. They show special characters correctly but when you import them via Import-CSV, special characters change.

I still do not understand why special characters show correctly in ANSI csv files but get corrupted once imported via Import-CSV. The workaround seems to indeed be to make sure they are stored as unicode.

Thanks!

 

Not Ranked
1 Posts

Great find! And perhaps for future reference, for Excel CSV files the workaround is to open up the .csv file in notepad and save it as unicode.

Not Ranked
8 Posts

Hi to all,

thank you very much for this information.

Does someone know, which Charset Name (UTF-8, -16, ...) or ISO import-csv using?

Top 10 Contributor
2,170 Posts
Microsoft MVP
Top Contributor

-Encoding<String>

Specifies the type of character encoding that was used in the CSV file. Valid values are Unicode, UTF7, UTF8, ASCII, UTF32, BigEndianUnicode, Default, and OEM. The default is ASCII.

http://technet.microsoft.com/en-us/library/hh849891.aspx

Not Ranked
8 Posts

I've solved my problem..

Set the "-Encoding UTF8" parameter on the import and export sideBig Smile:

(Get-Content -Encoding UTF8 C:\Temp\ads.txt) | % {$_ -replace '"', ""} | out-file -Encoding UTF8 -FilePath C:\temp\ads_UTF8.csv -Force

Top 10 Contributor
2,170 Posts
Microsoft MVP
Top Contributor

Good, 

next time please create new thread with appropriate name instead of adding to one that is solved (no marking of solved threads is here so follow your intuition).

In the new thread you can of course include link to any thread you want.

Page 1 of 1 (8 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.