unicode - How to print utf-8 to console with Python 3.4 (Windows 8)? -
I have ever used my format encoding and decoding other formats of Unicode (UTF-8, UTF-16, ASCI, etc.) ) But I have reached a wall in which both are confused and disappointing What I am trying to do is print UTF-8 card symbols (♠, ♥, ♦, ♣) from a python module to a Windows console. The console I'm using is git bash and I'm using console 2 as front-end I have tried / read many approaches below and have not worked anything yet tell me what I am doing I am possible and this is the right way to do it.
- Make sure the console UTF-8 characters can be handled. These two tests believe that the console is not a problem.
What am I doing
UTF-8 Unicode characters There is a byte encoding, trying to do this, print UTF-8 card symbols (♠, ♥, ♦, ♣) ♠ ♥ ♦ ♣ Unicode characters from a python module in a user console, which were reproduced in different encodings. And UTF-8 is one of those encoding - as a UTF, UTF-8 can reproduce any Unicode character Is obtained. But "UTF-8" is nothing special about those characters.
Other encodings that can reproduce the characters ♠ ♥ ♦ the, and that your console is likely to be used under Western European installations. You can print canc in these encodings but you are not using UTF-8 to do this, and you will not be able to use other Unicode characters available in UTF-8, but outside the scope of these code pages.
Unicode encode error: 'charmap' codec can not encode the character '\ u2660'
In Python 3 it is the same Which is different from what you did the test ('♠') that you have given this print
script against your py -3.4 How to apply / code>. What does
sys.stdout.encoding
give you with the script?
To work correctly, print
will work to ensure that the Python chooses the right encoding. If you are not doing this, you can actually get from PYTHONIOENCODING
to cp437
& gt; & Gt; & Gt; Must have to set. ; Text = '♠' & gt; & Gt; & Gt; Print (text .encode ('utf-8')) '\ xe2 \ x99 \ xa0'
print
can only print unicode wire For other types of string including
method, the literal representation of this object ( byte
string resulting from the encoded () repr
) is found b '\ xe2 \ X99 \ xa0 '
It happens that you write a Python literally 3 bytes in which UTF-8 is encoded.
If you want to do bypass by print
'encoding for piothone encoding and your own option, you can do it clearly:
gt; & Gt; & Gt; Import systems and gt; & Gt; & Gt; Sys.stdout.buffer.write ('♠' .encode ('cp437'))
This will certainly install any console code page 437 (e.g., non-Western-European installs) is). Generally, for applications that use C studio, such as Python, obtaining non-ASCII characters in the Windows console is very upset.
Comments
Post a Comment