与给定字符一样,python使用的是另一个字符。
\ is an escape character in Python
\t gets interpreted as a tab
当我打开文件test_file=open('c:\Python27\test.txt','r')
时。它给出了IOError: [Errno 22] invalid mode ('r') or filename: 'C:\Python27\test.txt'
错误。当我进行谷歌搜索时,我知道\t
在python中被解释为tab。同样明智的任何其他字符由python保留用于特定用途
答案 0 :(得分:1)
来自String literals section in Python language reference建议的by @Praveen:
除非存在
'r'
或'R'
前缀,否则转义序列 字符串根据类似于使用的规则解释 标准C.公认的逃逸顺序是:
+-----------------+---------------------------------+
| Escape Sequence | Meaning |
+=================+=================================+
| ``\newline`` | Ignored |
+-----------------+---------------------------------+
| ``\\`` | Backslash (``\``) |
+-----------------+---------------------------------+
| ``\'`` | Single quote (``'``) |
+-----------------+---------------------------------+
| ``\"`` | Double quote (``"``) |
+-----------------+---------------------------------+
| ``\a`` | ASCII Bell (BEL) |
+-----------------+---------------------------------+
| ``\b`` | ASCII Backspace (BS) |
+-----------------+---------------------------------+
| ``\f`` | ASCII Formfeed (FF) |
+-----------------+---------------------------------+
| ``\n`` | ASCII Linefeed (LF) |
+-----------------+---------------------------------+
| ``\N{name}`` | Character named *name* in the |
| | Unicode database (Unicode only) |
+-----------------+---------------------------------+
| ``\r`` | ASCII Carriage Return (CR) |
+-----------------+---------------------------------+
| ``\t`` | ASCII Horizontal Tab (TAB) |
+-----------------+---------------------------------+
| ``\uxxxx`` | Character with 16-bit hex value |
| | *xxxx* (Unicode only) |
+-----------------+---------------------------------+
| ``\Uxxxxxxxx`` | Character with 32-bit hex value |
| | *xxxxxxxx* (Unicode only) |
+-----------------+---------------------------------+
| ``\v`` | ASCII Vertical Tab (VT) |
+-----------------+---------------------------------+
| ``\ooo`` | Character with octal value |
| | *ooo* |
+-----------------+---------------------------------+
| ``\xhh`` | Character with hex value *hh* |
+-----------------+---------------------------------+