How to print non-ASCII characters in Python

I had a problem when I print (or write to a file) non-ASCII characters in Python. I solved this by overriding the method strin my own objects and making "x.encode (" utf-8 ") inside it, where x is the property inside the object.

But if I get a third-party object, and I do "str (object)", and this object has a non-ASCII character inside, it will fail.

So the question is: is there a way to tell the method strthat the object is UTF-8 encoded, in general? I am working with Python 2.5.4.

+3
source share
5 answers

, Unix-, var, :

export LC_CTYPE = "es: ES.UTF-8"

, utf-8, ,

+2

str() Unicode Python < 3.0.

repr(obj) str(obj). repr() ASCII, , ASCII.

, -, unicode. , :

fileObj = codecs.open( "someFile", "w", "utf-8" )

Unicode fileObj, . print, sys.stdout:

import sys, codecs, locale
print str(sys.stdout.encoding)
sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
line = u"\u0411\n"
print type(line), len(line)
sys.stdout.write(line)
print line
+6

How about using unicode(object)and defining a method __unicode__on your classes?

Then you know its Unicode, and you can encode it to a file anyway.

+3
source

just insert these two lines at the top of your code

  • #! / Usr / local / bin / python
  • # coding: latin-1

follow this link for more information https://www.python.org/dev/peps/pep-0263/

0
source
none_ascii = '''
        ███╗   ███╗ ██████╗ ██╗   ██╗██╗███████╗███████╗ 
        ████╗ ████║██╔═══██╗██║   ██║██║██╔════╝██╔════╝ 
        ██╔████╔██║██║   ██║██║   ██║██║█████╗  ███████╗ 
        ██║╚██╔╝██║██║   ██║╚██╗ ██╔╝██║██╔══╝  ╚════██║ 
        ██║ ╚═╝ ██║╚██████╔╝ ╚████╔╝ ██║███████╗███████║ 
        ╚═╝     ╚═╝ ╚═════╝   ╚═══╝  ╚═╝╚══════╝╚══════╝ 
'''

print(none_ascii.decode('utf-8'))
-1
source

Source: https://habr.com/ru/post/1722427/


All Articles