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..........(不带结尾")的内容,从而引发解析错误?
(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:
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.
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 \.
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
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.
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
What is the best way to represent a Windows directory, for example "C:\meshes\as"? I have been trying to modify a script but it never works because I can’t seem to get the directory right, I assume because of the '\' acting as escape character?
Yes, \ in Python string literals denotes the start of an escape sequence. In your path you have a valid two-character escape sequence \a, which is collapsed into one character that is ASCII Bell:
As you can see, in all these examples the backslash and the next character in the literal were grouped together to form a single character in the final string. The full list of Python’s escape sequences is here.
In Windows, you can use / in your path just like Linux or macOS in all places as long as you use PowerShell as your command-line interface. It comes pre-installed on Windows and it supports many Linux commands like ls command.
If you use Windows Command Prompt (the one that appears when you type cmd in Windows Start Menu), you need to specify paths with \ just inside it. You can use / paths in all other places (code editor, Python interactive mode, etc.).