155

When I try to print a Unicode string in a Windows console, I get an error .

UnicodeEncodeError: 'charmap' codec can't encode character ....

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?

Edit: I'm using Python 2.5.


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

@JFSebastian answer is more relevant as of today (6 Jan 2016).

James Sulak
  • 28,146
  • 11
  • 49
  • 55

13 Answers13

81

Update: Python 3.6 implements PEP 528: Change Windows console encoding to UTF-8: the default console on Windows will now accept all Unicode characters. Internally, it uses the same Unicode API as the win-unicode-console package mentioned below. print(unicode_string) should just work now.


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

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 

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

Windows console does accept Unicode characters and it can even display them (BMP only) if the corresponding font is configured. WriteConsoleW() API should be used as suggested in @Daira Hopwood's answer. It can be called transparently i.e., you don't need to and should not modify your scripts if you use win-unicode-console package:

T:\> py -m pip install win-unicode-console
T:\> py -m run your_script.py

See What's the deal with Python 3.4, Unicode, different languages and Windows?

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

If it is enough to replace all unencodable characters with ? in your case then you could set PYTHONIOENCODING envvar:

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.

Blairg23
  • 8,642
  • 5
  • 61
  • 61
jfs
  • 346,887
  • 152
  • 868
  • 1,518
  • 4
    "the default console on Windows will now accept all Unicode characters" **BUT** you need to configure the console: right click on the top of the windows (of the cmd or the python IDLE), in default/font choose the "Lucida console". (Japanese and Chinese don't work for me, but I should survive without it...) – JinSnow Jan 13 '17 at 20:46
  • 2
    @Guillaume: the answer contains the phrase in **bold** about Windows console: *"if the corresponding font is configured."* This answer doesn't mention IDLE but you don't need to configure the font in it (I see Japanese and Chinese characters just fine in IDLE by default. Try `print('\u4E01')`, `print('\u6b63')`). – jfs Jan 13 '17 at 21:14
  • 2
    @Guillaume You can even get Chinese if you install the language pack in Windows 10. It added console fonts that support Chinese. – Mark Tolonen Mar 12 '17 at 18:27
39

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


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

PrintFails - Python Wiki

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.

alvas
  • 94,813
  • 90
  • 365
  • 641
Lasse V. Karlsen
  • 350,178
  • 94
  • 582
  • 779
  • 7
    The link is dead and the gist of the answer wasn't quoted. -1 – 0xC0000022L Jan 11 '13 at 14:12
  • 1
    When I try the given advice about wrapping `sys.stdout`, it prints the wrong things. For example, `u'\u2013'` becomes `û` instead of an en-dash. – user2357112 supports Monica Jul 12 '14 at 22:39
  • @user2357112 You will have to post a new question about that. Unicode and system console is not necessarily the best combination, but I don't know enough about this, so if you need a definite answer, post a question here on SO about it. – Lasse V. Karlsen Jul 13 '14 at 12:05
  • 2
    the link is dead. The code example is wrong for Windows console where the codepage (OEM) such as `cp437` is different from Windows ANSI codepage such as `cp1252`. The code does not fix `UnicodeEncodeError: 'charmap' codec can't encode character` error and may lead to mojibake e.g., `ا©` is silently replaced with `╪º⌐`. – jfs Aug 24 '15 at 07:55
30

Despite the other plausible-sounding answers that suggest changing the code page to 65001, that does not work. (Also, changing the default encoding using sys.setdefaultencoding is not a good idea.)

See this question for details and code that does work.

Community
  • 1
  • 1
Daira Hopwood
  • 1,909
  • 18
  • 13
  • 2
    `win-unicode-console` Python package (based on your code) allows to avoid modifying your script if it prints Unicode directly using [`py -mrun your_script.py` command](http://stackoverflow.com/a/32176732/4279). – jfs Feb 27 '16 at 14:31
12

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.

Giampaolo Rodolà
  • 10,883
  • 5
  • 58
  • 57
  • `.encode('utf8').decode(sys.stdout.encoding)` leads to mojibake e.g., `u"\N{EM DASH}".encode('utf-8').decode('cp437')` -> `ΓÇö` – jfs Aug 24 '15 at 07:39
  • Simply `print(s.encode('utf-8'))` may be a better way to avoid compiler errors. Instead, you get \xNN output for unprintable characters, which was enough for my diagnostic messages. – CODE-REaD May 14 '16 at 17:25
  • 4
    This is enormously, *spectacularly* wrong. Encoding to UTF-8 then decoding as an 8-bit charset will a) often fail, not all codepages have characters for all 256 byte values, and b) *always* the wrong interpretation of the data, producing a [Mojibake](https://en.wikipedia.org/wiki/Mojibake) mess instead. – Martijn Pieters Jan 13 '17 at 18:08
10

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"
sorin
  • 137,198
  • 150
  • 472
  • 707
  • 1
    Is there a way to avoid this by just using a different console? – endolith Apr 16 '11 at 13:08
  • @sorin: Why do you first `import win32console` outside a `try` and later you do it conditionally inside a `try`? Isn't that kind of pointless (the first `import`) – 0xC0000022L Jan 11 '13 at 14:17
  • For what it's worth, the one provided by David-Sarah Hopwood works (I didn't get this one to even run because I haven't bothered installing the win32 extensions module) – Jaykul Feb 10 '13 at 04:11
  • 4
    Don't change the system default encoding; fix your Unicode values instead. Changing the default encoding can break libraries that rely on the, you know, *default behaviour*. There is a reason you have to force a module reload before you can do this. – Martijn Pieters May 15 '14 at 11:36
6

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")...!

mike rodent
  • 10,479
  • 10
  • 80
  • 104
  • Clever, a quick and dirty way to get around the issue. I think this is great for an intermittent solution. – Jacqlyn Jul 01 '16 at 20:23
6

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

chcp 65001 & set PYTHONIOENCODING=utf-8
c97
  • 541
  • 4
  • 9
3

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
Akshay
  • 2,052
  • 1
  • 20
  • 56
shubaly
  • 31
  • 2
2

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
Kinjal Dixit
  • 7,077
  • 2
  • 50
  • 65
  • 3
    `set PYTHONIOENCODING=UTF-8` may lead to [mojibake](http://goo.gl/QlkFXZ) if the console uses a different encoding such as cp437. [`cp65001` has various issues](http://bugs.python.org/issue1602). To print Unicode to Windows console, Unicode API should be used (`WriteConsoleW()`) as suggested in [my answer](http://stackoverflow.com/a/32176732/4279) where `PYTHONIOENCODING` is used only to replace characters that can't be represented in the current OEM code page with `?` (`WriteConsoleW()` works even for such characters). `PYTHONIOENCODING` can be used if the output is redirected to a file. – jfs Dec 26 '15 at 03:40
2

TL;DR:

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

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 ?.

2

The cause of your problem is NOT 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 in excellent the Dive into Python book

Csa77
  • 449
  • 9
  • 17
Bartosz Radaczyński
  • 17,696
  • 14
  • 48
  • 58
  • 2
    setdefaultencoding() is nolonger in sys (as of v2.0 according to the module docs). – Jon Cage Nov 04 '08 at 15:53
  • I cannot prove it right now, but I know that I've used this trick on a later version - 2.5 on Windows. – Bartosz Radaczyński Apr 09 '09 at 21:11
  • 6
    OK, after quite a while I have found out that: "This function is only intended to be used by the site module implementation and, where needed, by sitecustomize. Once used by the site module, it is removed from the sys module’s namespace." – Bartosz Radaczyński May 30 '09 at 20:43
  • Any encoding Python outputs must be matched by the console that going to be printing it. Setting the output encoding to UTF-8 is all very well, but if the console's not UTF-8 (and on Windows it won't ever be), you'll get garbage instead of non-ASCII characters. – bobince Sep 25 '10 at 13:51
  • 4
    actually you can set the windows console to be utf-8. you need to say chcp 65001 and it will be unicode. – Bartosz Radaczyński Sep 28 '10 at 19:25
  • 4
    To make it absolutely clear: **it is a is very a bad idea** to change the default encoding. This is akin to spalking your broken leg and walking on as if nothing happened, rather than have a doctor set the bone properly. All code handling Unicode text should do so consistently instead of relying on implicit encoding / decoding. – Martijn Pieters Dec 18 '14 at 23:19
1

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.

J. Does
  • 585
  • 1
  • 7
  • 20
0

James Sulak asked,

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

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)
    substitute:     print(str(text).encode('utf-8'))

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

  Halmalo n\xe2\x80\x99\xc3\xa9tait plus qu\xe2\x80\x99un point noir

Instead of

  Halmalo n’était plus qu’un point noir

Granted, the latter is preferable ceteris paribus, 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.

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

CODE-REaD
  • 2,153
  • 3
  • 25
  • 54