Python原始字符串和尾随反斜杠 [英] Python raw strings and trailing backslash

查看:202
本文介绍了Python原始字符串和尾随反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经碰到过一些东西,想知道这是一个Pythonbug还是至少是一个不正常的错误。如果有人知道这种行为的任何理由,我很好奇。我想到现在看起来像Python Code一样,迄今为止已经很愉快了。我只熟悉Python的2.x行。



原始字符串是前缀为 r 。这是非常好的,因为我可以在正则表达式中使用反斜杠,我不需要在任何地方加倍。在Windows上编写一次性的脚本也很方便,所以我也可以使用反斜杠。 (我知道我也可以使用正斜杠,但是一般的脚本通常包含从Windows的其他地方剪切的内容。)



太棒了!当然,除非你真的希望你的字符串以反斜杠结尾。没有办法在原始字符串中执行此操作。

 在[9]中:r'\\\
'
Out [9]:'\\\\
'

在[10]中:r'abc\\\
'
输出[10]:'abc\\\\
'

在[11]中:r'abc\'
-------------------------- ----------------------
文件< ipython console>,第1行
r'abc\'
^
语法错误:扫描字符串文字时的EOL


在[12]中:r'abc\\'
输出[12]:'abc所以在结束报价之前的一个反斜杠是一个错误,但是两个反斜杠给你两个反斜杠!当然,我不是唯一一个被这个困扰的人?



想到为什么原始字符串是原始的,除了反斜杠引号?我的意思是,如果我想在其中嵌入一个单引号,我只需要在字符串周围使用双引号,反之亦然。如果我想要两个,我只是三重报价。如果我真的想要一个原始字符串中的三个引号,那么我想我必须处理,但这被认为是适当的行为?



在Windows中的文件夹名称特别有问题,其中反斜杠是路径分隔符。

解决方案

这是一个常见问题



为了回应你真的希望你的字符串以反斜杠结尾,没有办法在一个'raw'字符串中这样做。解决它。

 >>> r'ab\c''\\'=='ab \\c\\''
True
>>>


I ran across something once upon a time and wondered if it was a Python "bug" or at least a misfeature. I'm curious if anyone knows of any justifications for this behavior. I thought of it just now reading "Code Like a Pythonista," which has been enjoyable so far. I'm only familiar with the 2.x line of Python.

Raw strings are strings that are prefixed with an r. This is great because I can use backslashes in regular expressions and I don't need to double everything everywhere. It's also handy for writing throwaway scripts on Windows, so I can use backslashes there also. (I know I can also use forward slashes, but throwaway scripts often contain content cut&pasted from elsewhere in Windows.)

So great! Unless, of course, you really want your string to end with a backslash. There's no way to do that in a 'raw' string.

In [9]: r'\n'
Out[9]: '\\n'

In [10]: r'abc\n'
Out[10]: 'abc\\n'

In [11]: r'abc\'
------------------------------------------------
   File "<ipython console>", line 1
     r'abc\'
           ^
SyntaxError: EOL while scanning string literal


In [12]: r'abc\\'
Out[12]: 'abc\\\\'

So one backslash before the closing quote is an error, but two backslashes gives you two backslashes! Certainly I'm not the only one that is bothered by this?

Thoughts on why 'raw' strings are 'raw, except for backslash-quote'? I mean, if I wanted to embed a single quote in there I'd just use double quotes around the string, and vice versa. If I wanted both, I'd just triple quote. If I really wanted three quotes in a row in a raw string, well, I guess I'd have to deal, but is this considered "proper behavior"?

This is particularly problematic with folder names in Windows, where the backslash is the path delimeter.

解决方案

It's a FAQ.

And in response to "you really want your string to end with a backslash. There's no way to do that in a 'raw' string.": the FAQ shows how to workaround it.

>>> r'ab\c' '\\' == 'ab\\c\\'
True
>>>

这篇关于Python原始字符串和尾随反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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