Python:语法错误:扫描字符串文字时 EOL
我在 s1="some very long string............"
中出现上述错误
有谁知道我做错了什么?
I have the above-mentioned error in s1="some very long string............"
Does anyone know what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
您不会在行尾之前放置
"
。如果您想执行此操作,请使用
"""
:You are not putting a
"
before the end of the line.Use
"""
if you want to do this:我遇到了这个问题 - 我最终发现原因是我在字符串中包含了
\
字符。如果您有其中任何一个,请使用\\
“转义”它们,它应该可以正常工作。I had this problem - I eventually worked out that the reason was that I'd included
\
characters in the string. If you have any of these, "escape" them with\\
and it should work fine.(假设您的字符串中没有/想要换行符...)
这个字符串到底有多长?
我怀疑从文件或命令行读取的行的长度是有限制的,并且因为行尾被截断,解析器会看到类似
s1="some very long string... .......
(没有结尾"
)并因此引发解析错误?您可以通过转义源中的换行符将长行分成多行,如下所示:
(Assuming you don't have/want line breaks in your string...)
How long is this string really?
I suspect there is a limit to how long a line read from a file or from the commandline can be, and because the end of the line gets choped off the parser sees something like
s1="some very long string..........
(without an ending"
) and thus throws a parsing error?You can split long lines up in multiple lines by escaping linebreaks in your source like this:
我遇到了类似的问题。我有一个字符串,其中包含 Windows 中文件夹的路径,例如
C:\Users\
问题是\
是转义字符,因此为了在字符串中使用它您需要再添加一个\
。错误:
C:\Users\
正确:
C:\\Users\\
I faced a similar problem. I had a string which contained path to a folder in Windows e.g.
C:\Users\
The problem is that\
is an escape character and so in order to use it in strings you need to add one more\
.Incorrect:
C:\Users\
Correct:
C:\\Users\\
在我的情况下,我的单引号字典字符串中有
\r\n
。我用\\r
替换了\r
的所有实例,用\\n
替换了\n
,它修复了我的问题问题,正确返回评估字典中转义的换行符。In my situation, I had
\r\n
in my single-quoted dictionary strings. I replaced all instances of\r
with\\r
and\n
with\\n
and it fixed my issue, properly returning escaped line breaks in the eval'ed dict.你可以试试这个:
You can try this:
我也遇到了这个问题,尽管这里有答案,但我想强调这一点
后
/
不能有空格,请注意I too had this problem, though there were answers here I want to an important point to this
after
/
there should not be empty spaces.Be Aware of it我也有这个确切的错误消息,对我来说,问题是通过添加“\”解决的。
事实证明,我的长字符串,分成大约八行,最后带有“\”,其中一个缺少“\”线。
Python IDLE 没有指定此错误所在的行号,但它以红色突出显示了完全正确的变量赋值语句,这让我很困惑。实际畸形的字符串语句(多行带有“\”)与突出显示的语句相邻。也许这会帮助别人。
I also had this exact error message, for me the problem was fixed by adding an " \"
It turns out that my long string, broken into about eight lines with " \" at the very end, was missing a " \" on one line.
Python IDLE didn't specify a line number that this error was on, but it red-highlighted a totally correct variable assignment statement, throwing me off. The actual misshapen string statement (multiple lines long with " \") was adjacent to the statement being highlighted. Maybe this will help someone else.
就我而言,我使用 Windows,因此必须使用双引号而不是单引号。
In my case, I use Windows so I have to use double quotes instead of single.
在我使用 Mac OS X 的情况下,我有以下声明:
我收到错误:
在我更改为:
它有效...
David
In my case with Mac OS X, I had the following statement:
I was getting the error:
After I change to:
It worked...
David
就我而言,我忘记了字符串末尾的 (' 或 ")。例如 'ABC' 或 "ABC"
In my case, I forgot (' or ") at the end of string. E.g 'ABC' or "ABC"
我在 postgresql 函数中收到此错误。我有一个很长的 SQL,为了更好的可读性,我用 \ 将其分成多行。然而,这就是问题所在。我删除了所有内容并将它们排成一行来解决问题。我使用的是 pgadmin III。
I was getting this error in postgresql function. I had a long SQL which I broke into multiple lines with \ for better readability. However, that was the problem. I removed all and made them in one line to fix the issue. I was using pgadmin III.
您的
变量(s1)
跨越多行。为了做到这一点(即您希望字符串跨越多行),您必须使用三引号(“””)。Your
variable(s1)
spans multiple lines. In order to do this (i.e you want your string to span multiple lines), you have to use triple quotes(""").下面的所有代码均使用 Python 3.8.3 进行测试
最简单 - 只需使用三引号。
无论是单:
还是双:
注意:三重引号字符串保留缩进,这意味着
和
或甚至只是
不一样。
有一个
textwrap.dedent
标准库中的 函数来处理这个问题,尽管使用它超出了问题的范围。您也可以在字符串内使用
\n
,驻留在单行上:此外,如果您的字符串中不需要任何换行符(即换行符),您可以使用
\
在常规字符串中:All code below was tested with Python 3.8.3
Simplest -- just use triple quotes.
Either single:
or double:
Note: triple quoted strings retain indentation, it means that
and
or even just
are not the same.
There is a
textwrap.dedent
function in standard library to deal with this, though working with it is out of question's scope.You can, as well, use
\n
inside a string, residing on single line:Also, if you don't need any linefeeds (i.e. newlines) in your string, you can use
\
inside regular string:在这种情况下,三个单引号或三个双引号都可以!
例如:
或
In this case, three single quotations or three double quotations both will work!
For example:
OR
我在访问任何硬盘驱动器目录时都遇到了同样的问题。
然后我就用这样的方法解决了。
上图显示了错误和已解决的输出。
I had faced the same problem while accessing any hard drive directory.
Then I solved it in this way.
The picture above shows an error and resolved output.
以前的大多数答案都是正确的,我的答案与 aaronasterling 非常相似,你也可以做 3 个单引号
s1='''一些很长的字符串............'''
Most previous answers are correct and my answer is very similar to aaronasterling, you could also do 3 single quotations
s1='''some very long string............'''
当代码中定义字符串的方式存在问题时,会出现错误“SyntaxError:扫描字符串文字时 EOL”。该错误消息表明在正确关闭字符串之前已到达字符串末尾。
注意:确保使用反斜杠字符 () 正确转义文件路径
The error "SyntaxError: EOL while scanning string literal" occurs when there is an issue with the way a string is defined in the code. The error message indicates that the end of the string was reached before the string was closed properly.
Note: make sure that the file paths are properly escaped using the backslash character ()
在窗口路径末尾使用 \ 即可正常工作,如下所示
use \ in the end of window path, and it will work just fine, as shown below
我收到此消息,但看不到任何字符串(在 IDLE windows python3.8 中):
哎呀,代码中出现了一个虚假的额外引号
这导致问题出现在看起来像未引用的文本中
I'm getting this message with no string in sight (in IDLE windows python3.8):
oops there was a spurious extra quote mark way back in the code
which was causing the pr0blem to show up in what looked like unquoted text
在我的情况下......
Python 在 Postgres 出现异常时抛出了这个错误。所有变量都存在于连接的字符串中,因此此错误根本不是由于 Python 中的问题造成的。 Postgres 存在竞争条件,因为在该 SQL 查询(使用此连接字符串)之前的一行有一个 SQL 查询。第一个 SQL 查询由于缺少所需的变量而失败,但 Python 代码返回正常(但不是 Postgres),但 Postgres 连接丢失或中断。因此,第二个 Postgres SQL 查询(下面的代码)无法运行。
In my situation...
Python threw this error during an anomaly with Postgres. All variables were present in the concatenated string, so this error was NOT due to an issue in Python at all. There was a race condition with Postgres because there was a SQL query one line before this line this SQL query (with this concatenated string). The first SQL query had failed due to a missing variable it needed but the Python code returned okay (but not Postgres), but the Postgres connection got lost or interrupted. Therefore, the second Postgres SQL query (the code below) couldn't run.