the reason: python string assignments accidentally change '\b' into '\x08' and '\a' into '\x07', why Python did this?
10170 просмотра
2 ответа
Had two answers and some comments, mentioned another question, but all had not provided REASON, why Python did this changes? such as '/b' is '/x08' is just the result, but why? 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.
after using : 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'
I then tested using the following code:mypath1='F:\big data\bython_coding\aiveintopython-5.4\ry'
the mypath1 now is : 'F:\x08ig data\x08ython_coding\x07iveintopython-5.4\ry'
all the '\b' changed into '\x08' and '\a' changed into '\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.
Автор: T.C Источник Размещён: 12.11.2019 09:26Ответы (2)
12 плюса
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'; such strings are called raw strings and use different rules for backslash escape sequences.
This is a historical usage dating from the early 60s. 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:
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.
7 плюса
Escape sequence \a
, \b
is equivalnt to \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'
Автор: falsetru
Размещён: 09.04.2014 11:34
Вопросы из категории :
- python Обработка XML в Python
- python Как я могу использовать Python itertools.groupby ()?
- python Python: На какой ОС я работаю?
- python Как я могу создать непосредственно исполняемое кроссплатформенное приложение с графическим интерфейсом на Python?
- python Вызов функции модуля с использованием его имени (строки)
- python Звук Питона («Колокол»)
- python Regex и unicode
- python Создать зашифрованный ZIP-файл в Python
- python Создайте базовый итератор Python
- python Функция транспонирования / распаковки (обратная сторона zip)?
- string В чем разница между строкой и строкой в ??C #?
- string Почему в Ruby нет реального StringBuffer или StringIO?
- string Преобразовать строку в перечисление в C #
- string Преобразование списка <Integer> в список <String>
- string Самый эффективный способ объединения строк?
- string Экранирование строк HTML с помощью jQuery
- string PHP эквивалент .NET / Java toString ()
- string Как отсортировать список строк?
- string Как сгенерировать случайную буквенно-цифровую строку?
- string Как разбирать строку в nullable int