Python:语法错误:扫描字符串文字时 EOL

发布于 2024-09-16 04:55:39 字数 89 浏览 14 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(21

爱你不解释 2024-09-23 04:55:39

您不会在行尾之前放置 "

如果您想执行此操作,请使用 """

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

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
"""
吃→可爱长大的 2024-09-23 04:55:39

我遇到了这个问题 - 我最终发现原因是我在字符串中包含了 \ 字符。如果您有其中任何一个,请使用 \\ “转义”它们,它应该可以正常工作。

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.

温折酒 2024-09-23 04:55:39

(假设您的字符串中没有/想要换行符...)

这个字符串到底有多长?

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

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

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:

s1="some very long string.....\
...\
...."
黑寡妇 2024-09-23 04:55:39

我遇到了类似的问题。我有一个字符串,其中包含 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\\

浊酒尽余欢 2024-09-23 04:55:39

在我的情况下,我的单引号字典字符串中有 \r\n 。我用 \\r 替换了 \r 的所有实例,用 \\n 替换了 \n ,它修复了我的问题问题,正确返回评估字典中转义的换行符。

ast.literal_eval(my_str.replace('\r','\\r').replace('\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.

ast.literal_eval(my_str.replace('\r','\\r').replace('\n','\\n'))
  .....
慈悲佛祖 2024-09-23 04:55:39

你可以试试这个:

s = r'long\annoying\path'

You can try this:

s = r'long\annoying\path'
灵芸 2024-09-23 04:55:39

我也遇到了这个问题,尽管这里有答案,但我想强调这一点

/ 不能有空格,请注意

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

往事风中埋 2024-09-23 04:55:39

我也有这个确切的错误消息,对我来说,问题是通过添加“\”解决的。

事实证明,我的长字符串,分成大约八行,最后带有“\”,其中一个缺少“\”线。

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.

绮烟 2024-09-23 04:55:39

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

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

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

C:\Users\Dr. Printer>python -mtimeit -s"a = 0"
100000000 loops, best of 3: 0.011 usec per loop
葬花如无物 2024-09-23 04:55:39

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

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

我收到错误:

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

在我更改为:

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

它有效...

David

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

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

雪花飘飘的天空 2024-09-23 04:55:39

就我而言,我忘记了字符串末尾的 (' 或 ")例如 'ABC'"ABC"

In my case, I forgot (' or ") at the end of string. E.g 'ABC' or "ABC"

戏剧牡丹亭 2024-09-23 04:55:39

我在 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.

撩心不撩汉 2024-09-23 04:55:39

您的变量(s1)跨越多行。为了做到这一点(即您希望字符串跨越多行),您必须使用三引号(“””)。

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

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(""").

s1="""some very long 
string............"""
心安伴我暖 2024-09-23 04:55:39

下面的所有代码均使用 Python 3.8.3 进行测试


最简单 - 只需使用三引号。

无论是单:

long_string = '''some
very 
long
string
............'''

还是双:

long_string = """some
very 
long
string
............"""

注意:三重引号字符串保留缩进,这意味着

long_string = """some
    very 
    long
string
............"""

long_string = """some
    very 
long
string
............"""

或甚至只是

long_string = """
some
very 
long
string
............"""

不一样

有一个 textwrap.dedent标准库中的 函数来处理这个问题,尽管使用它超出了问题的范围。


您也可以在字符串内使用 \n ,驻留在单行上:

long_string = "some \nvery \nlong \nstring \n............"

此外,如果您的字符串中不需要任何换行符(即换行符),您可以使用 \ 在常规字符串中:

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

All code below was tested with 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.


You can, as well, use \n inside a string, residing on single line:

long_string = "some \nvery \nlong \nstring \n............"

Also, if you don't need any linefeeds (i.e. newlines) in your string, you can use \ inside regular string:

long_string = "some \
very \
long \
string \
............"
幻想少年梦 2024-09-23 04:55:39

在这种情况下,三个单引号或三个双引号都可以!
例如:

    """Parameters:
    ...Type something.....
    .....finishing statement"""

    '''Parameters:
    ...Type something.....
    .....finishing statement'''

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'''
埖埖迣鎅 2024-09-23 04:55:39

我在访问任何硬盘驱动器目录时都遇到了同样的问题。
然后我就用这样的方法解决了。

 import os
 os.startfile("D:\folder_name\file_name") #running shortcut
 os.startfile("F:") #accessing directory

输入图片此处描述

上图显示了错误和已解决的输出。

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

enter image description here

The picture above shows an error and resolved output.

葬花如无物 2024-09-23 04:55:39

以前的大多数答案都是正确的,我的答案与 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............'''

早茶月光 2024-09-23 04:55:39

当代码中定义字符串的方式存在问题时,会出现错误“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 ()

瞳孔里扚悲伤 2024-09-23 04:55:39

在窗口路径末尾使用 \ 即可正常工作,如下所示

basePath = r'C:\Users\asaini2\OneDrive - Discover\arpan\Principal Data Science\git-project-config\\'

use \ in the end of window path, and it will work just fine, as shown below

basePath = r'C:\Users\asaini2\OneDrive - Discover\arpan\Principal Data Science\git-project-config\\'
梦屿孤独相伴 2024-09-23 04:55:39

我收到此消息,但看不到任何字符串(在 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

欲拥i 2024-09-23 04:55:39

在我的情况下......

Python 在 Postgres 出现异常时抛出了这个错误。所有变量都存在于连接的字符串中,因此此错误根本不是由于 Python 中的问题造成的。 Postgres 存在竞争条件,因为在该 SQL 查询(使用此连接字符串)之前的一行有一个 SQL 查询。第一个 SQL 查询由于缺少所需的变量而失败,但 Python 代码返回正常(但不是 Postgres),但 Postgres 连接丢失或中断。因此,第二个 Postgres SQL 查询(下面的代码)无法运行。

SyntaxError('EOL while scanning string literal', ('<string>', 1, 184, '"select table_name from information_schema.tables WHERE table_name LIKE \'" + table_prefix + "_%\' AND table_schema = \'" + schema + "\' and table_type = \'BASE TABLE\''))

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.

SyntaxError('EOL while scanning string literal', ('<string>', 1, 184, '"select table_name from information_schema.tables WHERE table_name LIKE \'" + table_prefix + "_%\' AND table_schema = \'" + schema + "\' and table_type = \'BASE TABLE\''))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文