python:SyntaxError:扫描字符串文字时EOL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3561691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:43:52  来源:igfitidea点击:

python: SyntaxError: EOL while scanning string literal

pythonstringstring-literals

提问by l--''''''---------''''''''''''

I have the above-mentioned error in s1="some very long string............"

我有上述错误 s1="some very long string............"

Does anyone know what I am doing wrong?

有谁知道我做错了什么?

采纳答案by aaronasterling

You are not putting a "before the end of the line.

您没有"在行尾放置 a 。

Use """if you want to do this:

"""如果您想这样做,请使用:

""" a very long string ...... 
....that can span multiple lines
"""

回答by JanC

(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?

我怀疑从文件或命令行读取的行的长度是有限制的,并且因为行的末尾被切掉,解析器会看到类似s1="some very long string..........(没有结尾")的东西,从而引发解析错误?

You can split long lines up in multiple lines by escaping linebreaks in your source like this:

您可以通过转义源代码中的换行符将长行分成多行,如下所示:

s1="some very long string.....\
...\
...."

回答by Chris H

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.

我遇到了这个问题 - 我最终发现原因是我\在字符串中包含了字符。如果你有任何这些,“逃避”他们,\\它应该可以正常工作。

回答by user12711

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.

Python IDLE 没有指定该错误所在的行号,但它以红色突出显示了一个完全正确的变量赋值语句,这让我很失望。实际畸形的字符串语句(多行带有“\”)与突出显示的语句相邻。也许这会帮助别人。

回答by madhu131313

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

我也有这个问题,虽然这里有答案,但我想在/不应该有空格之后强调一个重点 。注意它

回答by Nicolas Bouliane

In my situation, I had \r\nin my single-quoted dictionary strings. I replaced all instances of \rwith \\rand \nwith \\nand it fixed my issue, properly returning escaped line breaks in the eval'ed dict.

在我的情况下,\r\n我的单引号字典字符串中有。我替换了\rwith\\r\nwith 的所有实例,\\n它解决了我的问题,在 eval'ed dict 中正确返回了转义的换行符。

ast.literal_eval(my_str.replace('\r','\r').replace('\n','\n'))
  .....

回答by Ashish kulkarni

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 \.

我遇到了类似的问题。我有一个字符串,其中包含 Windows 中文件夹的路径,例如C:\Users\问题是这\是一个转义字符,因此为了在字符串中使用它,您需要再添加一个\

Incorrect: C:\Users\

不正确: C:\Users\

Correct: C:\\\Users\\\

正确的: C:\\\Users\\\

回答by Aminah Nuraini

In my case, I use Windows so I have to use double quotes instead of single.

就我而言,我使用 Windows,所以我必须使用双引号而不是单引号。

C:\Users\Dr. Printer>python -mtimeit -s"a = 0"
100000000 loops, best of 3: 0.011 usec per loop

回答by Ram Dwivedi

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.

我在 postgresql 函数中收到此错误。我有一个很长的 SQL,为了更好的可读性,我用 \ 分成了多行。然而,这就是问题所在。我删除了所有内容并将它们放在一行中以解决问题。我正在使用 pgadmin III。

回答by us_david

In my case with Mac OS X, I had the following statement:

在我使用 Mac OS X 的情况下,我有以下声明:

model.export_srcpkg(platform, toolchain, 'mymodel_pkg.zip', 'mymodel.dylib')

I was getting the error:

我收到错误:

  File "<stdin>", line 1
model.export_srcpkg(platform, toolchain, 'mymodel_pkg.zip', 'mymodel.dylib')
                                                                             ^
SyntaxError: EOL while scanning string literal

After I change to:

在我更改为:

model.export_srcpkg(platform, toolchain, "mymodel_pkg.zip", "mymodel.dylib")

It worked...

有效...

David

大卫