解决SyntaxError:在Python中扫描字符串文字时停产

Hello friends, In my last post I’ve discussed the common problem Python Programmers face i.e. unexpected EOF while parsing and in today’s post, I’ll be discussing EOL while scanning string literal.

朋友们,您好,在我的上一篇文章中,我讨论了Python程序员面临的常见问题,即在解析时出现意外的EOF ,在今天的帖子中,我将在扫描字符串文字时讨论EOL。

But do you exactly know what is this error all about and how we can remove it from our python code?

但是您是否确切知道此错误的全部原因以及如何从python代码中删除该错误?

So, what exactly an EOL is?

那么,什么是EOL?

EOL stands for End Of Line. This means we have now come to the end of our line and the error related to this is shown below with example code.

EOL表示行尾 。 这意味着我们现在已经到了行的结尾,并且与此相关的错误在下面显示示例代码。

def printName():
    return "This is a test basestring
    
printName()

Now, after running this piece of code, I get an error something similar to this.

现在,在运行这段代码之后,我得到了类似的错误。

File "main.py", line 2
  return "This is a test basestring
 
 
SyntaxError: EOL while scanning string literal
 
 
...Program finished with exit code 1
Press ENTER to exit the console
end of line error

Now the above code contains one error and the error is that the return in the printName( ) function in the above code is not closed properly. We can see that it doesn’t have the closing quote ".

现在,上面的代码包含一个错误,错误是上面的代码中的printName()函数中的返回未正确关闭。 我们可以看到它没有结束引号"

Now, when we tried to run this code, it showed us SyntaxError: EOL while scanning string literal.

现在,当我们尝试运行此代码时,它在扫描字符串文字时向我们显示了SyntaxError:EOL。

Now the problem is our return keyword has some syntax error. This lead to the SyntaxError: EOL while scanning string literal. To overcome this type of error in our python file, we need to ensure that we have not left any part of our string unclosed or not in proper syntax. I hope this is clear to you.

现在的问题是我们的return关键字有一些语法错误。 这导致在扫描字符串文字时出现SyntaxError:EOL。 为了克服python文件中的这种类型的错误,我们需要确保没有遗漏字符串的任何部分或未使用正确的语法。 希望您清楚。

Sometimes we try to print our string to multiple lines also and in that case what happens is that, let’s see this with an example:

有时我们也尝试将字符串打印到多行,在这种情况下会发生什么,让我们看一个例子:

def multiLine():
    return "This is a new line
                              and this is the next line"
multiLine()

Now, when we will try to run this code, it will give us the same error which we got above,

现在,当我们尝试运行此代码时,它将给我们与上面相同的错误,

eol multi line

To overcome this type of error while using the string spreading into multiple line, we can use the ''' or """. We can wrap our code into the triple quote. Both quotes single and double quote will work fine.

为了克服使用字符​​串扩展到多行时的这种类型的错误,我们可以使用'''""" 。我们可以将代码包装在三引号中,单引号和双引号都可以正常工作。

eol error multiline solution

Here you can see that our code is working fine and we are good to go.

在这里,您可以看到我们的代码运行良好,并且一切顺利。

Well, this was all about the EOL and the error related to it. If you stuck into any kind of error, don’t forget to google it and try to debug it on your own. This will teach you how to debug on your own as someone might have faced a similar problem earlier.

好吧,这一切都与EOL及其相关的错误有关。 如果您遇到任何类型的错误,请别忘了用Google搜索并尝试自行调试。 这将教您如何自行调试,因为之前可能有人遇到过类似的问题。

If you still don’t find any solution for your problem, you can ask your doubt in the comment’s section below and we’ll get back to you🤓.

如果您仍然找不到解决问题的方法,可以在下面的评论部分中提出疑问,我们会尽快与您联系🤓。

Thanks for your visit and if you are new here, consider subscribing to our newsletter. See you in my next post. Bye!

感谢您的访问,如果您是这里的新手,请考虑订阅我们的时事通讯。 在下一篇文章中见。 再见!

翻译自: https://www.thecrazyprogrammer.com/2020/05/solve-syntaxerror-eol-while-scanning-string-literal-in-python.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值