如何在Python中打印非ASCII字符

时间:2021-08-14 15:26:02

I have a problem when I'm printing (or writing to a file) the non-ASCII characters in Python. I've resolved it by overriding the str method in my own objects, and making "x.encode('utf-8')" inside it, where x is a property inside the object.

我在Python中打印(或写入文件)非ASCII字符时遇到问题。我已经通过覆盖我自己的对象中的str方法并在其中创建“x.encode('utf-8')”来解决它,其中x是对象内的属性。

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

但是,如果我收到第三方对象,并且我创建了“str(object)”,并且此对象内部有非ASCII字符,则它将失败。

So the question is: is there any way to tell the str method that the object has an UTF-8 codification, generically? I'm working with Python 2.5.4.

所以问题是:有没有办法告诉str方法该对象有一个UTF-8编码,一般来说?我正在使用Python 2.5.4。

5 个解决方案

#1


6  

There is no way to make str() work with Unicode in Python < 3.0.

没有办法让str()在Python <3.0中使用Unicode。

Use repr(obj) instead of str(obj). repr() will convert the result to ASCII, properly escaping everything that isn't in the ASCII code range.

使用repr(obj)代替str(obj)。 repr()会将结果转换为ASCII,正确转义不在ASCII代码范围内的所有内容。

Other than that, use a file object which allows unicode. So don't encode at the input side but at the output side:

除此之外,使用允许unicode的文件对象。所以不要在输入端编码,而是在输出端编码:

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

Now you can write unicode strings to fileObj and they will be converted as needed. To make the same happen with print, you need to wrap sys.stdout:

现在您可以将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

#2


3  

How about you use unicode(object) and define __unicode__ method on your classes?

你如何使用unicode(对象)并在类上定义__unicode__方法?

Then you know its unicode and you can encode it anyway you want into to a file.

然后你知道它的unicode,你可以将它编码到任何你想要的文件。

#3


2  

I would like to say that I've found a solution in Unix systems, exporting a environment var, with this:

我想说我在Unix系统中找到了一个解决方案,导出了一个环境var,用这个:

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

export LC_CTYPE =“es:ES.UTF-8”

This way, all files are in utf-8, so I can make prints or whatever and it works fine

这样,所有文件都是utf-8,所以我可以制作打印件或其他什么,它工作正常

#4


2  

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

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

#5


0  

just paste these two lines at the top of your code

只需将这两行粘贴到代码顶部即可

  1. #!/usr/local/bin/python
  2. #!的/ usr / local / bin目录/蟒蛇
  3. # coding: latin-1
  4. #coding:latin-1

go to this link for further details https://www.python.org/dev/peps/pep-0263/

请访问此链接了解更多详情https://www.python.org/dev/peps/pep-0263/

#1


6  

There is no way to make str() work with Unicode in Python < 3.0.

没有办法让str()在Python <3.0中使用Unicode。

Use repr(obj) instead of str(obj). repr() will convert the result to ASCII, properly escaping everything that isn't in the ASCII code range.

使用repr(obj)代替str(obj)。 repr()会将结果转换为ASCII,正确转义不在ASCII代码范围内的所有内容。

Other than that, use a file object which allows unicode. So don't encode at the input side but at the output side:

除此之外,使用允许unicode的文件对象。所以不要在输入端编码,而是在输出端编码:

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

Now you can write unicode strings to fileObj and they will be converted as needed. To make the same happen with print, you need to wrap sys.stdout:

现在您可以将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

#2


3  

How about you use unicode(object) and define __unicode__ method on your classes?

你如何使用unicode(对象)并在类上定义__unicode__方法?

Then you know its unicode and you can encode it anyway you want into to a file.

然后你知道它的unicode,你可以将它编码到任何你想要的文件。

#3


2  

I would like to say that I've found a solution in Unix systems, exporting a environment var, with this:

我想说我在Unix系统中找到了一个解决方案,导出了一个环境var,用这个:

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

export LC_CTYPE =“es:ES.UTF-8”

This way, all files are in utf-8, so I can make prints or whatever and it works fine

这样,所有文件都是utf-8,所以我可以制作打印件或其他什么,它工作正常

#4


2  

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

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

#5


0  

just paste these two lines at the top of your code

只需将这两行粘贴到代码顶部即可

  1. #!/usr/local/bin/python
  2. #!的/ usr / local / bin目录/蟒蛇
  3. # coding: latin-1
  4. #coding:latin-1

go to this link for further details https://www.python.org/dev/peps/pep-0263/

请访问此链接了解更多详情https://www.python.org/dev/peps/pep-0263/