简体   繁体   中英

Displaying Unicode Drawing Characters in Python

I have figured out how to use the characters I need but it works differently in the console on linux versus the windows console. I want to figure out if there is a way to resolve this where I can make it work on both or at least check to see what system it is running on and call the right code.

This is using the Coding Ground terminal at http://www.tutorialspoint.com/execute_python_online.php
It uses Fedora Release 21 and Python v2.7.5. This is my initial code:

# Unicode Test
print u'\u2554'

This gives me this error:

Traceback (most recent call last):                                                                                                                                                                                      
  File "main.py", line 2, in <module>                                                                                                                                                                                   
    print u'\u2554'                                                                                                                                                                                                     
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2554' in position 0: ordinal not in range(128) 

On Windows 7 console using Python 2.7.9 this runs correctly and prints the '╔' character. To make it run correctly on the linux terminal I have to use

# Unicode Test
print u'\u2554'.encode('UTF-8')

Unfortunately this code will not run correctly under the Windows console. If I use print u'\╔'.encode('UTF-8') on Windows I get b'\\xe2\\x95\\x94'

Any ideas on how I can make it run on both systems with out having to maintain different code for each console?

You need to change the default encoding of your terminal in linux, on terminal click right then from Terminal select Set Character Encoding then select Unicode (UTF-8) . for more info read http://www.ianbicking.org/illusive-setdefaultencoding.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM