繁体   English   中英

Python bug - 或者我的愚蠢 - EOL在扫描字符串文字时

[英]Python bug - or my stupidity - EOL while scanning string literal

我看不出以下两行之间的显着差异。

然而,第一次解析,后者则没有。

In [5]: n=""" \\"Axis of Awesome\\" """

In [6]: n="""\\"Axis of Awesome\\""""
  File "<ipython-input-6-d691e511a27b>", line 1
    n="""\\"Axis of Awesome\\""""
                                ^
SyntaxError: EOL while scanning string literal

这是一个Python bug / feature / oddity,还是我遗漏了一些基本的东西?

最后四个引号

"""\\"Axis of Awesome\\""""

被解析为""" ,即字符串的结尾,后跟" ,即开始新的字符串文字。 但是,这个新文字从未完成。 简单的例子:

>>> """foo""""bar"
'foobar'
>>> """foo""" "bar"
'foobar'

如果你想避免这个问题,那么用""" r'替换"""或者转义"

>>> """\\"Axis of Awesome\\\""""
'\\"Axis of Awesome\\"'
>>> r'\"Axis of Awesome\"'
'\\"Axis of Awesome\\"'

您的最后4个引号被评估为"" & ""而不是您期望将其评估为" & """

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM