为什么 Python 中的单个反斜杠原始字符串会导致语法错误? [英] Why does the single backslash raw string in Python cause a syntax error?

查看:42
本文介绍了为什么 Python 中的单个反斜杠原始字符串会导致语法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另请参阅为什么我不能用反斜杠结束原始字符串?为什么Python的原始字符串文字不能以单个反斜杠结尾? 问题和相关答案.

<小时>

在我的 Python 2 程序中,我使用了很多带有嵌入反斜杠的文字字符串.我可以使用另一个反斜杠来转义每个反斜杠(例如:"red\\blue")或使用 Python 原始字符串(例如:r"red\blue").我已经对原始字符串方法进行了标准化,该方法在除一种情况之外的所有情况下都能很好地工作.

如果我想表示一个双反斜杠,我可以使用 r"\\",但是如果我尝试输入一个单反斜杠文字 r"\" Python抱怨语法错误.显而易见的解决方法是在这种情况下使用 "\\",但是 为什么单个原始字符串反斜杠是错误的? 这是 Python 中的错误吗? 有没有办法将单个反斜杠编码为原始字符串?

示例:

<预><代码>>>>r"红色\蓝色"'红蓝'>>>r"\\"'\\\\'>>>r"\"文件<stdin>",第 1 行r"\"^语法错误:扫描字符串文字时 EOL>>>

我宁愿在整个程序中使用原始字符串保持一致,而在几个地方使用这个 "\\" 似乎是一种混乱.使用 r"\\"[0] 也好不到哪里去.我还考虑在程序开始时使用常量 BACKSLASH 其中 BACKSLASH = r"\\"[0] .又一个混蛋.

更新:当原始字符串末尾有奇数个反斜杠时,也会发生此错误.使用的默认字符串扫描器将反斜杠解释为转义字符,以便最后一个反斜杠将转义结束引号字符.本来可以将 "' 嵌入到字符串中,但是生成的字符串内部仍将带有反斜杠作为纯字符.

有几个与这个问题相关的问题,但没有一个答案解释为什么单个反斜杠原始字符串是一个错误或如何将单个反斜杠编码为原始字符串字符串:

python 用双反斜杠替换单反斜杠

无法打印\"(单反斜杠)在 Python 中

Python 正则表达式替换 double带有单个反斜杠的反斜杠

将双反斜杠转换为单反斜杠Python 3 中的反斜杠

为什么我不能用反斜杠结束原始字符串?

为什么Python的原始字符串不能文字以单个反斜杠结尾?

解决方案

这里的问题很简单,系统假设单反斜杠是为了转义,你是在转义你的引用,否则你必须转义转义字符.这会发生在任何让一个角色禁用另一个角色功能的环境中.

Also see the Why can't I end a raw string with a backslash? and Why can't Python's raw string literals end with a single backslash? questions and related answers.


In my Python 2 program I use a lot of literal strings with embedded backslashes. I could use another backslash to escape each of these backslashes (eg: "red\\blue") or use Python raw strings (eg: r"red\blue"). I have standardised on the raw string method which works well in all cases except one.

If I want to represent a double backslash, I can use r"\\", but if I try to enter a single backslash literal r"\" Python complains with a syntax error. The obvious workaround is to use "\\" in this case, but why is the single raw string backslash an error? Is this a bug in Python? Is there a way to code a single backslash as a raw string?

Example:

>>> r"red\blue"
'red\\blue'
>>> r"\\"
'\\\\'
>>> r"\"
  File "<stdin>", line 1
    r"\"
       ^
SyntaxError: EOL while scanning string literal
>>> 

I would rather be consistent using raw strings through-out the whole program, and using this "\\" in several places seems like a kludge. Using r"\\"[0] is not any better. I have also considered using a constant BACKSLASH where BACKSLASH = r"\\"[0] at the start of the program. Another kludge.

UPDATE: This error also happens when there is an odd number of backslashes at the end of a raw string. The default string scanner that is used interprets the backslash as an escape character, so that the last backslash will escape the ending quote character. It was intended that " and ' can be embedded in the string, however the resulting string will still have the backslashes inside as a plain character.

There are several questions related to this issue but none of the answers explains why the single backslash raw string is an error or how to code a single backslash as a raw string:

python replace single backslash with double backslash

can't print '\' (single backslash) in Python

Python regex to replace double backslash with single backslash

Convering double backslash to single backslash in Python 3

Why can't I end a raw string with a backslash?

Why can't Python's raw string literals end with a single backslash?

解决方案

The problem here is simple, the system assumes that the single backslash is for escaping, you are escaping your quote, otherwise you have to escape the escape char. This would happen in any environment that lets a character disable another characters function.

这篇关于为什么 Python 中的单个反斜杠原始字符串会导致语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆