1627

Python, Unicode, and the Windows console

Question:

When I try to print a Unicode string in a Windows console, I get a UnicodeEncodeError: 'charmap' codec can't encode character .... error. I assume this is because the Windows console does not accept Unicode-only characters. What's the best way around this? Is there any way I can make Python automatically print a ? instead of failing in this situation?

<strong>Edit:</strong> I'm using Python 2.5.

<hr />

<strong>Note:</strong> @LasseV.Karlsen answer with the checkmark is sort of outdated (from 2008). Please use the solutions/answers/suggestions below with care!!

<a href="https://stackoverflow.com/a/32176732/610569" rel="nofollow">@JFSebastian answer</a> is more relevant as of today (6 Jan 2016).

Answer1:

<strong>Note:</strong> This answer is sort of outdated (from 2008). Please use the solution below with care!!

<hr />

Here is a page that details the problem and a solution (search the page for the text <em>Wrapping sys.stdout into an instance</em>):

<a href="http://wiki.python.org/moin/PrintFails" rel="nofollow">PrintFails - Python Wiki</a>

Here's a code excerpt from that page:

$ python -c 'import sys, codecs, locale; print sys.stdout.encoding; \ sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout); \ line = u"\u0411\n"; print type(line), len(line); \ sys.stdout.write(line); print line' UTF-8 <type 'unicode'> 2 Б Б $ python -c 'import sys, codecs, locale; print sys.stdout.encoding; \ sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout); \ line = u"\u0411\n"; print type(line), len(line); \ sys.stdout.write(line); print line' | cat None <type 'unicode'> 2 Б Б

There's some more information on that page, well worth a read.

Answer2:

<strong>Update:</strong> <a href="https://docs.python.org/3.6/whatsnew/3.6.html#pep-528-change-windows-console-encoding-to-utf-8" rel="nofollow">Python 3.6</a> implements <a href="https://www.python.org/dev/peps/pep-0528/" rel="nofollow">PEP 528: Change Windows console encoding to UTF-8</a>: <em>the default console on Windows will now accept all Unicode characters.</em> Internally, it uses the same Unicode API as <a href="https://github.com/Drekin/win-unicode-console" rel="nofollow">the win-unicode-console package mentioned below</a>. print(unicode_string) should just work now.

<hr /><blockquote>

I get a UnicodeEncodeError: 'charmap' codec can't encode character... error.

</blockquote>

The error means that Unicode characters that you are trying to print can't be represented using the current (chcp) console character encoding. The codepage is often 8-bit encoding such as cp437 that can represent only ~0x100 characters from ~1M Unicode characters:

>>> u"\N{EURO SIGN}".encode('cp437') Traceback (most recent call last): ... UnicodeEncodeError: 'charmap' codec can't encode character '\u20ac' in position 0: character maps to <blockquote>

I assume this is because the Windows console does not accept Unicode-only characters. What's the best way around this?

</blockquote>

Windows console does accept Unicode characters and it can even display them (BMP only) <strong>if the corresponding font is configured</strong>. WriteConsoleW() API should be used as suggested in <a href="https://stackoverflow.com/a/4637795/4279" rel="nofollow">@Daira Hopwood's answer</a>. It can be called transparently i.e., you don't need to and should not modify your scripts if you use <a href="https://github.com/Drekin/win-unicode-console" rel="nofollow">win-unicode-console package</a>:

T:\> py -mpip install win-unicode-console T:\> py -mrun your_script.py

See <a href="https://stackoverflow.com/a/30551552/4279" rel="nofollow">What's the deal with Python 3.4, Unicode, different languages and Windows?</a>

<blockquote>

Is there any way I can make Python automatically print a ? instead of failing in this situation?

</blockquote>

If it is enough to replace all unencodable characters with ? in your case then you could set <a href="https://docs.python.org/3/using/cmdline.html#envvar-PYTHONIOENCODING" rel="nofollow">PYTHONIOENCODING envvar</a>:

T:\> set PYTHONIOENCODING=:replace T:\> python3 -c "print(u'[\N{EURO SIGN}]')" [?]

In Python 3.6+, the encoding specified by PYTHONIOENCODING envvar is ignored for interactive console buffers unless PYTHONLEGACYWINDOWSIOENCODING envvar is set to a non-empty string.

Answer3:

Despite the other plausible-sounding answers that suggest changing the code page to 65001, that <a href="http://bugs.python.org/issue1602" rel="nofollow">does not work</a>. (Also, changing the default encoding using sys.setdefaultencoding is <a href="https://stackoverflow.com/questions/3578685/how-to-display-utf-8-in-windows-console/3580165#3580165" rel="nofollow">not a good idea</a>.)

See <a href="https://stackoverflow.com/questions/878972/windows-cmd-encoding-change-causes-python-crash/3259271" rel="nofollow">this question</a> for details and code that does work.

Answer4:

If you're not interested in getting a reliable representation of the bad character(s) you might use something like this (working with python >= 2.6, including 3.x):

from __future__ import print_function import sys def safeprint(s): try: print(s) except UnicodeEncodeError: if sys.version_info >= (3,): print(s.encode('utf8').decode(sys.stdout.encoding)) else: print(s.encode('utf8')) safeprint(u"\N{EM DASH}")

The bad character(s) in the string will be converted in a representation which is printable by the Windows console.

Answer5:

The below code will make Python output to console as UTF-8 even on Windows.

The console will display the characters well on Windows 7 but on Windows XP it will not display them well, but at least it will work and most important you will have a consistent output from your script on all platforms. You'll be able to redirect the output to a file.

Below code was tested with Python 2.6 on Windows.

#!/usr/bin/python # -*- coding: UTF-8 -*- import codecs, sys reload(sys) sys.setdefaultencoding('utf-8') print sys.getdefaultencoding() if sys.platform == 'win32': try: import win32console except: print "Python Win32 Extensions module is required.\n You can download it from https://sourceforge.net/projects/pywin32/ (x86 and x64 builds are available)\n" exit(-1) # win32console implementation of SetConsoleCP does not return a value # CP_UTF8 = 65001 win32console.SetConsoleCP(65001) if (win32console.GetConsoleCP() != 65001): raise Exception ("Cannot set console codepage to 65001 (UTF-8)") win32console.SetConsoleOutputCP(65001) if (win32console.GetConsoleOutputCP() != 65001): raise Exception ("Cannot set console output codepage to 65001 (UTF-8)") #import sys, codecs sys.stdout = codecs.getwriter('utf8')(sys.stdout) sys.stderr = codecs.getwriter('utf8')(sys.stderr) print "This is an Е乂αmp١ȅ testing Unicode support using Arabic, Latin, Cyrillic, Greek, Hebrew and CJK code points.\n"

Answer6:

Like Giampaolo Rodolà's answer, but even more dirty: I really, really intend to spend a long time (soon) understanding the whole subject of encodings and how they apply to Windoze consoles,

For the moment I just wanted sthg which would mean my program would NOT CRASH, and which I understood ... and also which didn't involve importing too many exotic modules (in particular I'm using Jython, so half the time a Python module turns out not in fact to be available).

def pr(s): try: print(s) except UnicodeEncodeError: for c in s: try: print( c, end='') except UnicodeEncodeError: print( '?', end='')

NB "pr" is shorter to type than "print" (and quite a bit shorter to type than "safeprint")...!

Answer7:

For Python 2 try:

print unicode(string, 'unicode-escape')

For Python 3 try:

import os string = "002 Could've Would've Should've" os.system('echo ' + string)

Or try win-unicode-console:

pip install win-unicode-console py -mrun your_script.py

Answer8:

The cause of your problem is <strong>NOT</strong> the Win console not willing to accept Unicode (as it does this since I guess Win2k by default). It is the default system encoding. Try this code and see what it gives you:

import sys sys.getdefaultencoding()

if it says ascii, there's your cause ;-) You have to create a file called sitecustomize.py and put it under python path (I put it under /usr/lib/python2.5/site-packages, but that is differen on Win - it is c:\python\lib\site-packages or something), with the following contents:

import sys sys.setdefaultencoding('utf-8')

and perhaps you might want to specify the encoding in your files as well:

# -*- coding: UTF-8 -*- import sys,time

Edit: more info can be found <a href="http://www.diveintopython.net/xml_processing/unicode.html" rel="nofollow">in excellent the Dive into Python book</a>

Answer9:

Kind of related on the answer by J. F. Sebastian, but more direct.

If you are having this problem when printing to the console/terminal, then do this:

>set PYTHONIOENCODING=UTF-8

Answer10:

Python 3.6 windows7: There is several way to launch a python you could use the python console (which has a python logo on it) or the windows console (it's written cmd.exe on it).

I could not print utf8 characters in the windows console. Printing utf-8 characters throw me this error:

OSError: [winError 87] The paraneter is incorrect Exception ignored in: (_io-TextIOwrapper name='(stdout)' mode='w' ' encoding='utf8') OSError: [WinError 87] The parameter is incorrect

After trying and failing to understand the answer above I discovered it was only a setting problem. Right click on the top of the cmd console windows, on the tab font chose lucida console.

Answer11:

TL;DR:

print(yourstring.encode('ascii','replace')); <hr />

I ran into this myself, working on a Twitch chat (IRC) bot. (Python 2.7 latest)

I wanted to parse chat messages in order to respond...

msg = s.recv(1024).decode("utf-8")

but also print them safely to the console in a human-readable format:

print(msg.encode('ascii','replace'));

This corrected the issue of the bot throwing UnicodeEncodeError: 'charmap' errors and replaced the unicode characters with ?.

Answer12:

Just enter this code in command line before executing python script:

chcp 65001 & set PYTHONIOENCODING=utf-8

Answer13:

James Sulak asked,

<blockquote>

Is there any way I can make Python automatically print a ? instead of failing in this situation?

</blockquote>

Other solutions recommend we attempt to modify the Windows environment or replace Python's print() function. The answer below comes closer to fulfilling Sulak's request.

Under Windows 7, Python 3.5 can be made to print Unicode without throwing a UnicodeEncodeError as follows:

    In place of:    print(text)<br />     substitute:     print(str(text).encode('utf-8'))

Instead of throwing an exception, Python now displays unprintable Unicode characters as <em>\xNN</em> hex codes, e.g.:

  <em>Halmalo n\xe2\x80\x99\xc3\xa9tait plus qu\xe2\x80\x99un point noir</em>

Instead of

  <em>Halmalo n’était plus qu’un point noir</em>

Granted, the latter is preferable <em>ceteris paribus</em>, but otherwise the former is completely accurate for diagnostic messages. Because it displays Unicode as literal byte values the former may also assist in diagnosing encode/decode problems.

<strong>Note:</strong> The str() call above is needed because otherwise encode() causes Python to reject a Unicode character as a tuple of numbers.

Recommend

  • Displaying UTF8 stings in Ubuntu's terminal with a Python script
  • Python, Unicode, and the Windows console
  • Log flood after master upgrade from 1.6.13-gke.0 to 1.7.11-gke.1
  • Opengl Vertex Array Objects
  • What does JS $ mean?
  • How to run JSLint / JSHint in a file with templates?
  • Elisp internal variable scoping for a minor mode (setq/make-local-variable/let)
  • OO program and SQL database
  • Calling generic function with 'params' from F# (Observable.StartWith)
  • Best way to “overload” function in python? [duplicate]
  • Pointer help in C/C++ [closed]
  • Tcl extensions: Life Cycle of extensions' ClientData
  • Variable value in foreach of Red language
  • How to use threads in Perl?
  • How do I create parametrized XPath queries in SQL server?
  • How can I get information about who called a method?
  • How to discover what is available in lua environment?
  • Create a postgreSQL database programmatically [closed]
  • Cycles in Gremlin/Cypher
  • Channel mix with Pillow
  • Using QProcess.finished() in Python 3 and PyQt
  • How to export Selenium Test Suite/Case as C#
  • Finding regular expressions for languages otherwise described
  • Does Microsoft chatbot(Node.js) support multiple language in the single LUIS.AI application?
  • Maven archetype generate with custom properties
  • Can statically compiled languages replace scripting language?
  • C# Application Relative Paths
  • How to install PyAutoGUI
  • import error with python-mysql-connector 1.16, django 1.6, and python 3.2.3
  • How do I put a semicolon in a value in python configparser?
  • Development workflow for server and client using Docker Compose?
  • What is the difference between Socket.Send and Stream.Write? (in relation to tcp ip connections)
  • Error while importing scikits.talkbox
  • Flex/AS3 very strange simple Number operation issue
  • How to debug Shell command after customization
  • Why can't I use non-integral types with switch [duplicate]
  • cygwin cannot exec 'git-add--interactive' permission denied
  • Checking free space on FTP server
  • R: gsub and capture
  • AT Commands to Send SMS not working in Windows 8.1