0

I need to print some unicode characters in Python 2.7.

Why does the following code gave me an error?

print(u'\u2601')

Output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2601' in position 0: ordinal not in range(128)
FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
  • Working on Ubuntu 12.04, Python 2.7.3. – Celeo Oct 09 '14 at 19:08
  • @Celeo You can reproduce this error via http://repl.it/languages/Python, for example – FrozenHeart Oct 09 '14 at 19:10
  • Is this of any use: http://stackoverflow.com/questions/492483/setting-the-correct-encoding-when-piping-stdout-in-python? – NPE Oct 09 '14 at 19:10
  • It depends on your locale; in your case, Python assumes standard output needs to be ASCII-encoded, rather than UTF-8 encoded. – chepner Oct 09 '14 at 19:15
  • Normally, Python detects the console encoding. Using `ascii` implies the locale isn't set up properly, or the output is being piped somewhere besides the console. – Mark Tolonen Oct 10 '14 at 02:24
  • `repl.it` console just doesn't support Unicode. Neither does the Windows console (actually it's a more complicated story in that case, but that's the upshot for most people). https://wiki.python.org/moin/PrintFails – bobince Oct 11 '14 at 14:29

0 Answers0