Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Emit some Utf-8 string to windows console [duplicate]

Possible Duplicate:
Python, Unicode, and the Windows console

I read some strings from file and when I try to print these utf-8 strings in windows console, I get error

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 0: ordinal not in range(128)

I've tried to set console-encoding to utf-8 with "chcp 65001" But than I than get this error message

LookupError: unknown encoding: cp65001
like image 767
Meloun Avatar asked Apr 25 '12 18:04

Meloun


2 Answers

I recommend you to check similar questions on stackoverflow, there are many of them.

Anyway, you can do it this way:

  1. read from file in any encoding (for example utf8) but decode strings to unicode
  2. for windows console, output unicode strings. You don't need to encode in this special case. You don't need to set the console encoding, output text will be correctly encoded automatically.

For files, you need to use codecs module or to encode in proper encoding.

like image 126
Jiri Avatar answered Sep 28 '22 00:09

Jiri


The print command tries to convert Unicode strings to the encoding supported by the console. Try:

>>> import sys
>>> sys.stdout.encoding
'cp852'

It shows you what encoding the console supports (what is told to Python to be supported). If the character cannot be converted to that encoding, there is no way to display it correctly.

like image 22
pepr Avatar answered Sep 28 '22 00:09

pepr