【问题标题】:Using """ and escape sequences in Python 3?在 Python 3 中使用“””和转义序列?
【发布时间】:2018-04-29 12:40:30
【问题描述】:

我现在正在学习 Python,想尝试所有转义序列,但我的代码似乎有错误。请帮我找到它?

我的代码:

string = """backslash - \\\nsingle quote - \'\ndouble quote - \"
ASCII bell - \a Lorem
ASCII backspace - \b Lorem
ACII formfeed - \f Lorem
Unicode - \N{prabhu} Ipsum
Carriage return - \r Ipsum
16 bit Hex value - \u1234
32 bit Hex value - \U12345678 ASCII Vertical tab - \v
Octal value character - \123
Hex value - \x12 
"""

print(string)

--> 错误:SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 137-146: unknown unicode character name

【问题讨论】:

  • 那个位置是否表示\N{prabhu}的部分?
  • 肯定是\N{prabhu}。这不是 unicode 字符名称。你想得到什么符号?如果你用一些有效的 unicode 字符名称替换它,它就可以完美地工作。

标签: python python-3.x


【解决方案1】:

罪魁祸首是\N{prabhu}\U12345678

\U12345678 是非法的,因为它远远低于有效范围限制 (0010ffff)。十六进制的12345678305419896,其中unicode 以1114111 (0010ffff) 结尾。

\N{prabhu} 是非法的,因为它不是有效的 Unicode 字符名称。

【讨论】:

  • 我是这么认为的 :) 正确的 Unicode 名称示例?
  • 我知道;)(有效列表取决于 Python 的特定版本)。我的意思是你可以在你的答案中加入一个正确的例子,让它更完整。
猜你喜欢
  • 2023-03-15
  • 2021-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多