原始字符串和\ [英] raw strings and \

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

问题描述

我以为我理解原始字符串,然后我被这个咬了:


x = r''c:\ blah \''


这是非法的!我认为\在原始的

字符串中没有特别的含义所以为什么它不能成为最后一个字符?


让我这样做:


x = r''c:\ blah''''\\''


真傻......

解决方案

写道:

我以为我理解原始字符串,然后我被这个咬了:

x = r''c:\ blah \''

这是非法的!我认为\在原始的字符串中没有特别的意思,所以为什么它不能成为最后一个字符呢?


不,反斜杠在解析字符串方面仍然很特殊,它只需要解析字符串后转义序列不是
/>
解释。

让我这样做:

x = r''c:\ blah''''\\'' />
只是愚蠢......



是的,这太傻了。你也可以这样做:


x =''c:/ blah /''





x =''c:\\\\\\


但是为什么你想用文字反斜杠结束你的道路呢?

养成使用os.path模块进行所有操作

文件路径的习惯,你再也不用担心这种事了。


这种东西应该运作得很好:


folder =''c:/ blah''


for line in open (os.path.join(文件夹,''data.txt'')):

打印行


嗨Duncan,

感谢您的回复。我认为这是一个技术问题

与解析器相关联。


这个是我的Python陷阱列表。从最终用户的角度来看,非常愚蠢的是,原始字符串中的\不是特别的_except_

如果它是最后一个字符。


< PL **** @ alumni.caltech.edu>写道:

嗨Duncan,

感谢您的回复。我认为这是一个与解析器相关的技术问题。

这个问题正在我的Python陷阱列表中。从最终用户的角度来看,如果它是最后一个字符,那么在原始字符串_except_
中不是特别愚蠢。




另一种方法是根本不提供原始字符串

来包含其引用字符。由于rawstrings被设计为支持正则表达式(从不需要以反斜杠结束),

被认为是更高的成本 - 没有真正的优势

支持反斜杠 - 使用Dos / Windows路径文字,即使很多

Windowsers也使用rawstrings。

Alex


I thought I understood raw strings, then I got bitten by this:

x=r''c:\blah\''

which is illegal! I thought that \ had no special meanning in a raw
string so why can''t it be the last character?

making me do:

x=r''c:\blah'' ''\\''

is just silly...

解决方案

wrote:

I thought I understood raw strings, then I got bitten by this:

x=r''c:\blah\''

which is illegal! I thought that \ had no special meanning in a raw
string so why can''t it be the last character?
No, the backslash is still special in terms of parsing the string, it
is simply that once the string has been parsed escape sequences are not
interpreted.

making me do:

x=r''c:\blah'' ''\\''

is just silly...


Yes, that is silly. You could alternatively do:

x = ''c:/blah/''

or

x = ''c:\\blah\\''

But why would you want to end your path with a literal backslash anyway?
Get into the habit of using the os.path module for all manipulations of
file paths and you never need to worry about this sort of thing again.

This sort of thing should work well enough:

folder = ''c:/blah''

for line in open(os.path.join(folder, ''data.txt'')):
print line


Hi Duncan,

thanks for the reply. I figured that this was a technical problem
associated with the parser.

This one is going on my Python gotchas list. It is really silly from
an end user perspective to have \ not special in raw strings _except_
if it is the last character.


<pl****@alumni.caltech.edu> wrote:

Hi Duncan,

thanks for the reply. I figured that this was a technical problem
associated with the parser.

This one is going on my Python gotchas list. It is really silly from
an end user perspective to have \ not special in raw strings _except_
if it is the last character.



The alternative would have been to offer no way at all for a rawstring
to include its quoting-character. Since rawstrings were designed to
support regular expressions (which never need to end with a backslash),
that was considered a higher cost -- there is no real advantage to
supporting backslash-using Dos/Windows path literals, even though many
Windowsers use rawstrings for those.
Alex


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

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