[英]the reason: python string assignments accidentally change '\b' into '\x08' and '\a' into '\x07', why Python did this?
Had two answers and some comments, mentioned another question, but all had not provided REASON, why Python did this changes? 有两个答案和一些评论,提到了另一个问题,但都没有提供REASON,为什么Python做了这个改变? such as '/b' is '/x08' is just the result, but why? 例如'/ b'是'/ x08'只是结果,但为什么呢? Cheers. 干杯。
I try to add this path"F:\\big data\\Python_coding\\diveintopython-5.4\\py" into sys.path, therefore, the code under it could be imported directly. 我尝试将此路径“F:\\ big data \\ Python_coding \\ diveintopython-5.4 \\ py”添加到sys.path中,因此,可以直接导入其下的代码。
after using : sys.path.append('F:\\big data\\Python_coding\\diveintopython-5.4\\py')
使用后: sys.path.append('F:\\big data\\Python_coding\\diveintopython-5.4\\py')
I found I had this path inside sys.path: 'F:\\x08ig data\\Python_coding\\diveintopython-5.4\\py' 我发现我在sys.path中有这条路径:'F:\\ x08ig data \\ Python_coding \\ diveintopython-5.4 \\ py'
I then tested using the following code: mypath1='F:\\big data\\bython_coding\\aiveintopython-5.4\\ry'
然后我使用以下代码进行测试: mypath1='F:\\big data\\bython_coding\\aiveintopython-5.4\\ry'
the mypath1 now is : 'F:\\x08ig data\\x08ython_coding\\x07iveintopython-5.4\\ry'
mypath1现在是: 'F:\\x08ig data\\x08ython_coding\\x07iveintopython-5.4\\ry'
all the '\\b' changed into '\\x08' and '\\a' changed into '\\x07' 所有'\\ b'变为'\\ x08','\\ a'变为'\\ x07'
I searched for a while, but still can not find the reason, could you please check it out and any feedback or help will be appropriated. 我搜索了一会儿,但仍然找不到原因,请你检查一下,任何反馈或帮助都会被挪用。 Many thanks. 非常感谢。
Your strings are being escaped. 你的字符串被转义了。 Check out the docs on string literals : 查看有关字符串文字的文档:
The backslash () character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. 反斜杠()字符用于转义具有特殊含义的字符,例如换行符,反斜杠本身或引号字符。 String literals may optionally be prefixed with a letter
r' or
R'; 字符串文字可以选择以字母r' or
R'为前缀; such strings are called raw strings and use different rules for backslash escape sequences. 这样的字符串称为原始字符串,并对反斜杠转义序列使用不同的规则。
This is a historical usage dating from the early 60s. 这是一个可以追溯到60年代初的历史用法。 It allows you to enter characters that you're not otherwise able to enter from a standard keyboard. 它允许您输入您无法从标准键盘输入的字符。 For example, if you type into the Python interpreter: 例如,如果您输入Python解释器:
print "\xDC"
...you'll get Ü
. ......你会得到Ü
。 In your case, you have \\b
- representing backspace - which Python displays in the \\xhh
form, where hh
is the hexadecimal value for 08. \\a
is the escape sequence for the ASCII bell: try print "\\a"
with your sound on and you should hear a beep. 在你的情况下,你有\\b
- 表示退格 - 哪个Python以\\xhh
形式显示,其中hh
是08的十六进制值。 \\a
是ASCII铃声的转义序列:尝试用你的声音print "\\a"
你应该听到一声哔哔声。
Escape sequence \\a
, \\b
is equivalnt to \\x07
, \\x08
. 转义序列\\a
, \\b
等于\\x07
, \\x08
。
>>> '\a'
'\x07'
>>> '\b'
'\x08'
You should escape \\
itself to represent backslash literally: 你应该逃脱\\
本身来代表反斜杠:
>>> '\\a'
'\\a'
>>> '\\b'
'\\b'
or use raw string literals: 或使用原始字符串文字:
>>> r'\a'
'\\a'
>>> r'\b'
'\\b'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.