Python, Unicode, and the Windows console -


When I try to print Unicode string in a Windows console, I get a Unicode encoding error : The 'charmap' codec character can not be encoded .... Error I believe this because the Windows console does not accept Unicode-only characters. What is the best way around this? Either way I would like to have Python automatically instead of failing in this situation? Can I print?

Edit: I am using Python 2.5.


Note: @Leserve Answer with Karelson Checkmark is outdated (since 2008)

is more relevant today (6 January 2016).

note: This answer is out of date (since 2008) please be careful Use the solution below!


Here is a page that gives details of the problem and the solution (search the page wrapping sys.stdout in an example for the text): < P>

There is a code excerpt from that page:

  $ python -c 'import system, codec, locale; Print sys.stdout.encoding; \ Sys.stdout = codecs.getwriter (locale.getpreferredcoding ()) (sys.stdout); \ Line = u "\ u0411 \ n"; Print type (line), lane (line); \ Sys.stdout.write (line); Print Line 'UTF-8 & lt; Type 'Unicode' & gt; 2 Б Б $ python -c 'import system, codec, locale; Print sys.stdout.encoding; \ Sys.stdout = codecs.getwriter (locale.getpreferredcoding ()) (sys.stdout); \ Line = u "\ u0411 \ n"; Print type (line), lane (line); \ Sys.stdout.write (line); Print line '| No cat & lt; Type 'Unicode' & gt; 2b Б  

There is more information on that page, one is worth reading.


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -