gpt4 book ai didi

python - 为什么 '\x' 在 Python 中无效?

转载 作者:IT老高 更新时间:2023-10-28 20:21:26 24 4
gpt4 key购买 nike

我正在尝试使用 '\' 字符,使用 '\a\b\c...' 只是为了为自己枚举 Python 将哪些字符解释为控制字符,以及解释为什么。这是我发现的:

\a - BELL
\b - BACKSPACE
\f - FORMFEED
\n - LINEFEED
\r - RETURN
\t - TAB
\v - VERTICAL TAB

我尝试过的大多数其他字符,'\g'、'\s' 等只是计算为反斜杠和给定字符的 2 个字符的字符串。我知道这是故意的,对我来说很有意义。

但是 '\x' 是个问题。当我的脚本到达这个源代码行时:

val = "\x"

我明白了:

ValueError: invalid \x escape

'\x' 有什么特别之处?为什么它与其他非转义字符的处理方式不同?

最佳答案

documentation 中有一个表格列出了所有转义码及其含义。 .

Escape Sequence    Meaning                        Notes\xhh               Character with hex value hh    (4,5)

Notes:

4. Unlike in Standard C, exactly two hex digits are required.
5. In a string literal, hexadecimal and octal escapes denote the byte with the given value; it is not necessary that the byte encodes a character in the source character set. In a Unicode literal, these escapes denote a Unicode character with the given value.

关于python - 为什么 '\x' 在 Python 中无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2704654/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com