[ python-Bugs-1530012 ] Literal strings use BS as octal escape character
SourceForge.net
noreply at sourceforge.net
Mon Aug 14 16:53:12 CEST 2006
Bugs item #1530012, was opened at 2006-07-28 08:08
Message generated for change (Comment added) made by ncoghlan
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1530012&group_id=5470
Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Submitted By: brucepeterson (brucepeterson)
Assigned to: Nobody/Anonymous (nobody)
Summary: Literal strings use BS as octal escape character
Initial Comment:
Also in 2.4
Using a literal to hard code a path. My directory
happened to start with a number and I couldn't open
the file due to the bad directory name. Found that
the tripple quote was operating as documented.
I would have at least expected the tripple double
quotes to not have an escape character. (Is this a
pep?) (From my reading of the Introduction, the
triple double quotes should act like a raw string
except that you can have a single double quote
included in the string.)
-------------
code snippet:
-------------
dir1 = """C:\1stDirecotry"""
dir2 = '''C:\2ndDirecotry'''
dir3 = '''C:\9thDirecotry'''
print dir1, dir2, dir3
C:☺stDirecotry C:☻ndDirecotry C:\9thDirecotry
dir1's format was not expected, dir2's format might be
expected.
>>> '''\1'''
'\x01'
>>> '''\9'''
'\\9'
----------------------------------------------------------------------
>Comment By: Nick Coghlan (ncoghlan)
Date: 2006-08-15 00:53
Message:
Logged In: YES
user_id=1038590
Looking at the tutorial section on strings, it's almost
certainly worth moving the introduction of triple-quoted
string above the introduction of raw strings. At the moment,
I can see how the OP could get the idea that the description
of raw strings applied to triple-quoted strings as well.
The other thing to do would be to explicitly mention
backslash escaping problems in Section 7.2 where it
discusses opening files, and the two possible solutions (use
forward slashes in path literals regardless of platform, or
else used doubled backslashes). I actually thought this was
in the docs already, but it doesn't appear to be in any of
the even vaguely obvious places (the files section in the
tutorial, or the descriptions of file(), open(), file type
or the os.path module in the library reference).
----------------------------------------------------------------------
Comment By: Neal Norwitz (nnorwitz)
Date: 2006-07-29 13:25
Message:
Logged In: YES
user_id=33168
Triple quotes are just like single or double quotes wrt
escaping. You need to prefix the string with an r:
r"C:\1stDirecotry" to get what you want.
This is probably documented in many places. I'm not sure
where you looked or where you expected it to be documented.
Can you suggest improvements?
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1530012&group_id=5470
More information about the Python-bugs-list
mailing list