gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:23:22 24 4
gpt4 key购买 nike

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

但是第一个解析,而第二个不解析。

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 错误/功能/奇怪之处,还是我遗漏了一些基本知识?

最佳答案

中的最后四个引号

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

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

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

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

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

关于Python 错误 - 或者我的愚蠢 - 在扫描字符串文字时 EOL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11328335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com