[英]python: SyntaxError: EOL while scanning string literal
I have the above-mentioned error in s1="some very long string............"
我在
s1="some very long string............"
有上述错误s1="some very long string............"
Does anyone know what I am doing wrong?有谁知道我做错了什么?
You are not putting a "
before the end of the line.你没有在行尾放一个
"
。
Use """
if you want to do this:如果要执行此操作,请使用
"""
:
""" a very long string ......
....that can span multiple lines
"""
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.如果您有任何这些,请使用
\\\\
“转义”它们,它应该可以正常工作。
(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.....\
...\
...."
In my situation, I had \\r\\n
in my single-quoted dictionary strings.在我的情况下,我的单引号字典字符串中有
\\r\\n
。 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.我用
\\\\r
替换了\\r
所有实例,用\\\\r
\\n
替换了\\\\n
并且它解决了我的问题,在 eval'ed dict 中正确返回了转义的换行符。
ast.literal_eval(my_str.replace('\r','\\r').replace('\n','\\n'))
.....
I faced a similar problem.我遇到了类似的问题。 I had a string which contained path to a folder in Windows eg
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\\\\\\
你可以试试这个:
s = r'long\annoying\path'
我也有这个问题,虽然这里有答案,但我想在此之后强调一个重要的点/
不应该有空格。注意它
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. Python IDLE 没有指定该错误所在的行号,但它以红色突出显示了一个完全正确的变量赋值语句,这让我很失望。 The actual misshapen string statement (multiple lines long with " \\") was adjacent to the statement being highlighted.
实际畸形的字符串语句(多行带有“\\”)与突出显示的语句相邻。 Maybe this will help someone else.
也许这会帮助别人。
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
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大卫
I was getting this error in postgresql function.我在 postgresql 函数中收到此错误。 I had a long SQL which I broke into multiple lines with \\ for better readability.
我有一个很长的 SQL,为了更好的可读性,我用 \\ 分成了多行。 However, that was the problem.
然而,这就是问题所在。 I removed all and made them in one line to fix the issue.
我删除了所有内容并将它们放在一行中以解决问题。 I was using pgadmin III.
我正在使用 pgadmin III。
就我而言,我忘记了字符串末尾的(' 或 ") 。例如 'ABC'或"ABC"
Your variable(s1)
spans multiple lines.您的
variable(s1)
跨越多行。 In order to do this (ie you want your string to span multiple lines), you have to use triple quotes(""").为了做到这一点(即你希望你的字符串跨越多行),你必须使用三重引号(""")。
s1="""some very long
string............"""
In this case, three single quotations or three double quotations both will work!在这种情况下,三个单引号或三个双引号都可以使用! For example:
例如:
"""Parameters:
...Type something.....
.....finishing statement"""
OR或者
'''Parameters:
...Type something.....
.....finishing statement'''
I had faced the same problem while accessing any hard drive directory.我在访问任何硬盘驱动器目录时遇到了同样的问题。 Then I solved it in this way.
然后我就这样解决了。
import os
os.startfile("D:\folder_name\file_name") #running shortcut
os.startfile("F:") #accessing directory
The picture above shows an error and resolved output.上图显示了错误和已解决的输出。
以前的大多数答案都是正确的,我的答案与 aaronasterling 非常相似,您也可以做 3 个单引号 s1='''some very long string .........'''
All code below was tested with Python 3.8.3以下所有代码均使用 Python 3.8.3 进行测试
Simplest -- just use triple quotes.最简单 - 只需使用三重引号。
Either single:要么单身:
long_string = '''some
very
long
string
............'''
or double:或双倍:
long_string = """some
very
long
string
............"""
Note: triple quoted strings retain indentation, it means that注意:三引号字符串保留缩进,这意味着
long_string = """some
very
long
string
............"""
and和
long_string = """some
very
long
string
............"""
or even just甚至只是
long_string = """
some
very
long
string
............"""
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.标准库中有一个
textwrap.dedent
函数来处理这个问题,尽管使用它超出了问题的范围。
You can, as well, use \\n
inside a string, residing on single line:您也可以在字符串中使用
\\n
,驻留在单行上:
long_string = "some \nvery \nlong \nstring \n............"
Also, if you don't need any linefeeds (ie newlines) in your string, you can use \\
inside regular string:此外,如果您的字符串中不需要任何换行符(即换行符),您可以在常规字符串中使用
\\
:
long_string = "some \
very \
long \
string \
............"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.