r" \“是什么问题? [英] what is wrong with that r"\"

查看:90
本文介绍了r" \“是什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无言的问题:


>> r" \"



文件"< stdin>",第1行

r" \"

^

语法错误:扫描单引号字符串时的EOL


>> r" \"



''\\''

解决方案

7月3日上午7:15,alf< ask @mewrote:


无言问题:


>> r" \"



文件"< stdin>",第1行

r" \"

^

语法错误:扫描单引号字符串时的EOL


>> r" \



''\\''



一个斜杠转义后续字符,所以

写它的正确方法是


r" \\或者r" \""


参见 http://docs.python.org/ref/strings.html 了解更多信息。


Mike


7月3日上午8:25,Jean-Paul Calderone< exar ... @ divmod.comwrote:


On星期二,2007年7月3日06:16:43 -0700,kyoso ... @ gmail.com写道:


7月3日上午7:15,alf< ;问@mewrote:


无字问题:


>> r" \"

文件"< stdin>",第1行

r" \" ;

^

语法错误:扫描单引号字符串时的EOL

>> r" \"

''\\''


一个斜杠转义后续字符,因此写入它的正确方式是


r" \\"或者r" \""


Seehttp://docs.python.org/ref/strings.html了解更多信息。



我想知道OP是否在询问如何拼写单长字符串\?

在这种情况下,答案是它不能用原始字符串来完成,

但是\\可以。反斜杠转义不是用原始字符串解释的,

但是你仍然不能用反斜杠结束原始字符串。


Jean-Paul



非常......有时候我需要阅读这些奇怪的帖子2或3次。


Mike


在2007-07-03,alf< ask @mewrote:


无字的问题:


> r" \"



文件"< stdin>",第1行

r" \"

^

语法错误:扫描单引号字符串时的EOL


> r" \



''\\''



来自Python语言参考2.4 .1字符串文字:


当r时或R或R。前缀存在,字符串中包含

反斜杠后面的字符没有更改,所有

反斜杠都保留在字符串中。例如,字符串

literal r" \ n"由两个字符组成:反斜杠和

小写n。字符串引号可以用反斜杠转义,

但反斜杠仍然在字符串中;例如,r" \""是
一个有效的字符串文字,由两个字符组成:

反斜杠和双引号; R" \"不是有效的字符串

literal(即使是原始字符串也不能以奇数

反斜杠结尾)。具体来说,原始字符串不能以

单个反斜杠结束(因为反斜杠会在引用字符后面转移

)。另请注意,单个反斜杠

后跟换行符被解释为这两个字符

作为字符串的一部分,而不是续行。


-

Neil Cerutti

询问我们拥有房屋的计划 - 在抵押贷款公司签名


question without words:

>>r"\"

File "<stdin>", line 1
r"\"
^
SyntaxError: EOL while scanning single-quoted string

>>r"\ "

''\\ ''

解决方案

On Jul 3, 7:15 am, alf <ask@mewrote:

question without words:

>>r"\"

File "<stdin>", line 1
r"\"
^
SyntaxError: EOL while scanning single-quoted string

>>r"\ "

''\\ ''

One slash escapes the following character, so the proper way of
writing it is either

r"\\" or r"\""

See http://docs.python.org/ref/strings.html for more information.

Mike


On Jul 3, 8:25 am, Jean-Paul Calderone <exar...@divmod.comwrote:

On Tue, 03 Jul 2007 06:16:43 -0700, kyoso...@gmail.com wrote:

On Jul 3, 7:15 am, alf <ask@mewrote:

question without words:

>>r"\"
File "<stdin>", line 1
r"\"
^
SyntaxError: EOL while scanning single-quoted string
>>r"\ "
''\\ ''

One slash escapes the following character, so the proper way of
writing it is either

r"\\" or r"\""

Seehttp://docs.python.org/ref/strings.htmlfor more information.


I wonder if the OP was asking how to spell the one-length string \?
In that case, the answer is that it can''t be done using raw strings,
but "\\" does it. Backslash escapes aren''t interpreted in raw strings,
but you still can''t end a raw string with a backslash.

Jean-Paul

Very true...sometimes I need to read these weird posts 2 or 3 times.

Mike


On 2007-07-03, alf <ask@mewrote:

question without words:

>r"\"

File "<stdin>", line 1
r"\"
^
SyntaxError: EOL while scanning single-quoted string

>r"\ "

''\\ ''

From the Python Language Reference 2.4.1 String Literals:

When an "r" or "R" prefix is present, a character following a
backslash is included in the string without change, and all
backslashes are left in the string. For example, the string
literal r"\n" consists of two characters: a backslash and a
lowercase "n". String quotes can be escaped with a backslash,
but the backslash remains in the string; for example, r"\"" is
a valid string literal consisting of two characters: a
backslash and a double quote; r"\" is not a valid string
literal (even a raw string cannot end in an odd number of
backslashes). Specifically, a raw string cannot end in a
single backslash (since the backslash would escape the
following quote character). Note also that a single backslash
followed by a newline is interpreted as those two characters
as part of the string, not as a line continuation.

--
Neil Cerutti
Ask about our plans for owning your home --sign at mortgage company


这篇关于r&quot; \“是什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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