问题:python:SyntaxError:扫描字符串文字时停产

我有上述错误 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?


回答 0

您没有"在行尾放置a 。

"""如果要执行此操作,请使用:

""" 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
"""

回答 1

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

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.


回答 2

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

这串真的多久了?

我怀疑从文件或从命令行读取的行的长度是有限制的,并且由于行的结尾被解析器截断,因此会看到类似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.....\
...\
...."

回答 3

在我的情况下,我\r\n在单引号中包含字典字符串。我取代的所有实例\r\\r\n\\n它固定我的问题,正确地返回在eval’ed字典逃脱换行符。

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'))
  .....

回答 4

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


回答 5

我也有这个问题,尽管这里有答案,但我想在/不应该有空白的地方对此做一个重要 说明。

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


回答 6

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

事实证明,我的长字符串在结尾处被分解成大约八行,并带有“ \”,但在一行上缺少“ \”。

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.


回答 7

就我而言,我使用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

回答 8

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


回答 9

就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")

有效…

大卫

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


回答 10

您可以尝试以下方法:

s = r'long\annoying\path'

You can try this:

s = r'long\annoying\path'

回答 11

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

回答 12

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

    """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'''

回答 13

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


回答 14

访问任何硬盘目录时,我都遇到了同样的问题。然后我以这种方式解决了。

 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.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。