Proper way to print unicode characters to the console in Python when using inline scripts -


i looking way print unicode characters utf-8 aware linux console, using python 2.x's print method.

what is:

$ python2.7 -c "print u'é'" é 

what want:

$ python2.7 -c "print u'é'" é 

python detects correctly console configured utf-8.

$ python2.7 -c "import sys; print sys.stdout.encoding" utf-8 

i have looked @ 11741574, proposed solution uses sys.stdout, whereas looking solution using print.

i have looked @ 5203105, using encode method not fix anything.

$ python -c "print u'é'.encode('utf8')" é 

solutions

as suggested @klausd. , @itzmeontv

$ python2.7 -c "print 'é'" é 

as suggested @pm2ring

$ python -c "# coding=utf-8 > print u'é'" é 

see accepted answer explanation cause of issue.

the problem isn't printing console, problem interpreting -c argument command line:

$ python -c "print repr('é')" '\xc3\xa9' # ok, expected byte string $ python -c "print repr('é'.decode('utf-8'))" u'\xe9' # ok, byte string decoded explicitly $ python -c "print repr(u'é')" u'\xc3\xa9' # bad, decoded implicitly iso-8859-1 

seems problem python doesn't know encoding command line arguments using, same kind of problem if source code file had wrong encoding. in case tell python encoding source used coding comment, , can here too:

$ python -c "# coding=utf-8 print repr(u'é')" u'\xe9' 

generally i'd try avoid unicode on command line though, if might ever have run on windows story worse.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -