python使用的字符列表(字符串文字中的转义序列)

时间:2016-01-08 07:28:59

标签: python-2.7

与给定字符一样,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保留用于特定用途

1 个答案:

答案 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*   |
+-----------------+---------------------------------+