Back to Top

使用Python原义标识符遇到的小问题

常见编程语言中字符串存在转义字符, 造成写 正则表达式 或 Windows路径 等等含有转义字符的字符串时需要处理, 所以有的语言提供了原义标识符(verbatim string literal).

相同之处

Python是以r开头的字符串声明作为其原义标识符:

# python
s1 = "c:\\documents\\files\\u0066.txt"
s2 = r"c:\documents\files\u0066.txt"

两个字符串的值是:

c:\documents\files\u0066.txt
c:\documents\files\u0066.txt

同样的输出, 在其他语言中写法:

// C++11
auto s1 = "c:\\documents\\files\\u0066.txt";
auto s2 = R"(c:\documents\files\u0066.txt)";
// C#
var s1 = "c:\\documents\\files\\u0066.txt";
var s2 = @"c:\documents\files\u0066.txt";
-- lua
var s1 = "c:\\documents\\files\\u0066.txt";
var s2 = @"c:\documents\files\u0066.txt";

不同之处1

# python
s1 = "He said, \"This is the last \u0063hance\x0021\""
s2 = r"He said, ""This is the last \u0063hance\x0021"""
He said, "This is the last chance21"
He said, This is the last chance21

而其他语言中

// C++11
auto s1 = "He said, \"This is the last \u0063hance\x0021\"";
auto s2 = R"(He said, ""This is the last \u0063hance\x0021"")";
// C#
var s1 = "He said, \"This is the last \u0063hance\x0021\"";
var s2 = @"He said, ""This is the last \u0063hance\x0021""";
-- lua
s1 = "He said, \"This is the last \u0063hance\x0021\""
s2 = [[He said, ""This is the last \u0063hance\x0021""]]
He said, "This is the last u0063hancex0021"
He said, ""This is the last \u0063hance\x0021""

不同之处2

# python
s1 = "c:\\documents\\"
s2 = r"c:\documents\"
File "main.py", line 2
    s2 = r"c:\documents\"
                        ^
SyntaxError: EOL while scanning string literal

而其他语言中

// C++11
auto s1 = "c:\\documents\\";
auto s2 = R"(c:\documents\)";
// C#
var s1 = "c:\\documents\\";
var s2 = @"c:\documents\";
-- lua
s1 = "c:\\documents\\"
s2 = [[c:\documents\]]
var s1 = "c:\\documents\\"
var s2 = [[c:\documents\]]        

差异的原因 TODO