简体   繁体   English

原因:python字符串赋值意外地将'\\ b'更改为'\\ x08'而将'\\ a'更改为'\\ x07',为什么Python会这样做?

[英]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.

相关问题 Python 3.5-将bytes对象转换为16位十六进制字符串(b'\\ x07 \\ x89'->'0x0789') - Python 3.5 - Convert bytes object to 16bit hex string (b'\x07\x89' -> '0x0789') Python Pyodbc Binary Column返回\\ xda \\ x08 \\ xcd \\ x08 \\ xba \\ x08而不是数字 - Python Pyodbc Binary Column is returning \xda\x08\xcd\x08\xba\x08 instead of numbers Pycrypto AES解密导致额外的“ \\ x07 \\ x07 \\ x07 \\ x07 \\ x07 \\ x07 \\ x07'” - Pycrypto AES decryption resulting in extra “\x07\x07\x07\x07\x07\x07\x07'” how to store bytes like b'PK\x03\x04\x14\x00\x08\x08\x08\x009bwR\x00\x00\x00\x00\x00\x00\x00 to dataframe or csv in python - how to store bytes like b'PK\x03\x04\x14\x00\x08\x08\x08\x009bwR\x00\x00\x00\x00\x00\x00\x00 to dataframe or csv in python 如何将退格\\ x08应用于字符串? - How to apply backspace \x08 to a string? python如何将特殊字符串u'\\ x08'u'\\ x09'u'\\ x03'转换为8 9 3 - python how to convert Special string u'\x08' u'\x09' u'\x03' to 8 9 3 我如何将像 b'\\x08\\x00\\x00\\x00' 这样的 Python/socket 读取的雷达数据解码为数字 - How do I decode radar data read by Python/socket like b'\x08\x00\x00\x00' to numbers 如何在python中看起来像[8,200,1,16]的字节格式与看起来像b'\\ x08 \\ xc8 \\ x01 \\ x10 \\ xc8的字节格式之间转换 - How to convert between the byte format that looks like [8, 200, 1, 16] to the one that looks like b'\x08\xc8\x01\x10\xc8 in python 为什么对两个十六进制字符串进行异或运算会产生以下格式的输出:'\\ x02 \\ x07 \\ x01P \\ x00 \\ x07 - Why XORing two hex strings produce an output in the following format: '\x02\x07\x01P\x00\x07 为什么`print'1 \\ x08'`结果1同时`print'1 \\ x082'`结果为2? - Why `print '1\x08'` results 1 meanwhile `print '1\x082'` results 2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM