Python 错误 - 或者我的愚蠢 - 扫描字符串文字时 EOL [英] Python bug - or my stupidity - EOL while scanning string literal
本文介绍了Python 错误 - 或者我的愚蠢 - 扫描字符串文字时 EOL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看不出以下两行之间的显着差异.
然而第一个解析,而后者没有.
在 [5]: n=""" \\"Axis of Awesome\\" """在 [6] 中:n="""\\"真棒轴\\""""文件<ipython-input-6-d691e511a27b>",第 1 行n="""\\"真棒轴\\""""^语法错误:扫描字符串文字时 EOL
这是 Python 错误/功能/奇怪,还是我遗漏了一些基本的东西?
解决方案
最后四个引号
"""\\"真棒轴\\""""
被解析为 """
,即字符串的结尾,后跟 "
,即新字符串文字的开始.但是,这个新文字永远不会完成.简单例子:
如果你想避免这个问题,那么用 r'
替换 """
或者转义 "
:
I cannot see a significant difference between the two following lines.
Yet the first parses, and the latter, does not.
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
Is this a Python bug/feature/oddity, or have I missing something fundamental?
解决方案
The last four quote marks in
"""\\"Axis of Awesome\\""""
are parsed as """
, i.e. end of string, followed by "
, i.e. start of a new string literal. This new literal is never completed, though. Simple example:
>>> """foo""""bar"
'foobar'
>>> """foo""" "bar"
'foobar'
If you want to avoid this problem, then replace """
with r'
or escape the "
:
>>> """\\"Axis of Awesome\\\""""
'\\"Axis of Awesome\\"'
>>> r'\"Axis of Awesome\"'
'\\"Axis of Awesome\\"'
这篇关于Python 错误 - 或者我的愚蠢 - 扫描字符串文字时 EOL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文