python3.0 raw string疑问

bearnedzq 2010-10-28 10:48:59
python3.0 raw string疑问

我想用raw string表达 abc\

>>> r = r'abc\'
SyntaxError: EOL while scanning string literal
>>> r = r'abc\''
>>> print(r)
abc\'

结果python不是很聪明,在raw string里\还是表现出转义的功能。
后面要再加上一个',才是合法的字符串。可是这样,已经不是我要的结果了。

怎样用 raw string 表达出 abc\ ?
结论似乎是不可能,非得用 r = 'abc\\' 这样丑陋的方式
看来 raw string 还得再改进才成
...全文
110 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
I_NBFA 2010-10-28
  • 打赏
  • 举报
回复
Even in a raw string, string quotes can be escaped with a backslash, but the backslash remains in the string; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.

python文档里写的很清楚rawstring会识别引号转义但原样保留字符,所以不能以\结尾。
解决办法很简单,r"abc" + '\\'
feilniu 2010-10-28
  • 打赏
  • 举报
回复
http://docs.python.org/py3k/reference/lexical_analysis.html#literals

Even in a raw string, string quotes can be escaped with a backslash, but the backslash remains in the string; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.


http://hongqn.livejournal.com/13175.html

37,738

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧