Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python3中使用原始unicode转义编码打印字符串?_Python_Unicode_Python 3.x - Fatal编程技术网

如何在Python3中使用原始unicode转义编码打印字符串?

如何在Python3中使用原始unicode转义编码打印字符串?,python,unicode,python-3.x,Python,Unicode,Python 3.x,在Python3.x中,以下代码的类型错误为:必须是str,而不是bytes,因为现在encode()返回bytes,而print()只需要str #!/usr/bin/python from __future__ import print_function str2 = "some unicode text" print(str2.encode('raw_unicode_escape')) 如何使用print()打印Unicode字符串转义表示?我正在寻找一个可以与Python2.6或更高版

在Python3.x中,以下代码的类型错误为:必须是str,而不是bytes,因为现在
encode()
返回
bytes
,而
print()
只需要
str

#!/usr/bin/python
from __future__ import print_function
str2 = "some unicode text"
print(str2.encode('raw_unicode_escape'))
如何使用
print()
打印Unicode字符串转义表示?我正在寻找一个可以与Python2.6或更高版本(包括3.x)一起使用的解决方案

更新 下一行将适用于3.x,但不适用于2.6,生成
AttributeError:“file”对象没有属性“buffer”

sys.stdout.buffer.write(str2.encode('raw_unicode_escape'))

我无法重现您的问题,请查看我的尝试日志(这解释了我在评论中的链接)

然而:

似乎您试图在写入文件时通过自己做所有的腿部工作来强制编码。然而,在Python 3中,接受一个
编码
参数,它为您发挥了所有的魔力

badp@delta:~$ python3
Python 3.1.2 (r312:79147, Apr 15 2010, 12:35:07) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> foo = open("look mah, utf-32", "w", encoding="utf-32")
>>> foo.write("bar")
3
>>> foo.close()
>>> foo = open("look mah, utf-32", "rb")
>>> foo.read()
b'\xff\xfe\x00\x00b\x00\x00\x00a\x00\x00\x00r\x00\x00\x00'
如果您正在寻找与Python 2相当的版本,那么您似乎真的想使用它。

作为repr(),返回包含对象的可打印表示形式的字符串,但使用\x、\u或\u转义符转义repr()返回的字符串中的非ASCII字符。这将生成一个类似于Python 2中repr()返回的字符串

结果字符串的类型确实是
str
,而不是
bytes

例如:

>>> a = '''Ⴊ ⇠ ਐ ῼ இ ╁ ଠ ୭ ⅙ ㈣'''
>>> ascii(a)
"'\\u10aa \\u21e0 \\u0a10 \\u1ffc \\u0b87 \\u2541 \\u0b20 \\u0b6d \\u2159 \\u3223'"
>>> print(ascii(a))
'\u10aa \u21e0 \u0a10 \u1ffc \u0b87 \u2541 \u0b20 \u0b6d \u2159 \u3223'
如果你想删掉多余的引号,你可以只做
print(ascii(a)[1:-1])

编辑:正如Alex所说,您必须在Python2.6中使用
repr
,而不是
ascii
。他的解决方案确实适用于Python2和Python3,但如果您计划进行大量转换(因此希望更容易多次键入),一种可能是在程序的开头添加一个条件,如下所示:

import sys
if sys.version_info[0] == 3:
    unic = ascii
else:
    unic = repr
然后,只要在Python 2中使用
repr
,在Python 3中使用
ascii
,就可以使用
unic
(或者你想叫它什么名字)

…虽然我想如果您想更小心一点,您可以使用
elif sys.version\u info[0]==2:
而不是
,否则:

我只会使用:

print(str2.encode('raw_unicode_escape').decode('ascii'))

如果您希望Python3和Python2.6中的代码完全相同(否则,您可以在2.6中使用
repr
,在Python3中使用
ascii
,但这并不是真正的“完全相同”;-)。

尝试从文件内部运行,这样做是行不通的。从控制台运行可以工作,但不能从文件运行。此外,我还添加了一条关于缓冲区使用的新注释。
ascii
不在2.6中。@Alex:True,但如我回答中的引号所示,
repr
是。
repr
在Python 3中不会用转义符替换非ascii字符——根据语言级别使用两个不同的函数(
repr
在Python2中,ascii在Python3中)并不是OP所需要的,“一个可以与Python2.6或更高版本(包括3.x)一起工作的解决方案”@Alex:用一个简单的解决方案更新了我的答案。谢谢Alex,我目前正在为Python 2.6+/3.x构建一组函数重写,以使其更加Unicode友好。我希望我会成功。有没有关于如何重写file.write函数以使其接受字节的想法?它与@Sorin有关,如果您不想,通常会进行类型检查o接受unicode字符串和字节字符串,并以不同的方式对待它们;有时,您可以通过调整(例如,一个方法接受unicode字符串并返回不变的字符串,或者一个字节字符串并返回通过解码获得的unicode字符串)--但这与评论中容易讨论的内容有点遥远,与原始问题有很大不同,因此你可能想问另一个单独的问题!