r'\U'给出SyntaxError:(unicode错误)'rawUnicodeScape'编解码器无法解码字节

2024-04-19 12:22:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是python2.7。你知道吗

test = r'\U' 

提供:

SyntaxError: (unicode error) 'rawunicodeescape' codec can't decode bytes in position 0-1: truncated \uXXXX

我以为原始字符串应该忽略反斜杠。不?你知道吗

我想要一个实际包含“\”和“U”的字符串。你知道吗

如果有关系,我还使用:

from __future__ import (absolute_import, division, print_function, unicode_literals)

Tags: 字符串intestimportbytesunicodepositionerror
1条回答
网友
1楼 · 发布于 2024-04-19 12:22:39

Python 2原始unicode字符串有一个bizarre quirk,它们仍然处理\u\U序列:

When an 'r' or 'R' prefix is used in conjunction with a 'u' or 'U' prefix, then the \uXXXX and \UXXXXXXXX escape sequences are processed while all other backslashes are left in the string.

文档描述了组合ru前缀的行为,但我相信这也适用于组合r前缀和unicode_literals。你知道吗

相关问题 更多 >