473,799 Members | 3,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

raw strings and \

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...

Mar 5 '06 #1
20 2268
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.jo in(folder, 'data.txt')):
print line
Mar 5 '06 #2
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.

Mar 5 '06 #3
<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
Mar 5 '06 #4
pl****@alumni.c altech.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.


But at the parsing stage, it *is* somewhat special, even if it is not the
last charater. E.g,

a = r'check \' this'
print a

The second quote is not regarded as ending the string literal.


Mar 5 '06 #5
On Sun, 05 Mar 2006 08:27:31 -0800, plahey 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.


I don't deny that this is a gotcha, but you misunderstand the purpose of
raw strings. They weren't designed so that Windows users could enter
pathnames with backslashes. Raw strings are designed to enter regular
expressions, and for regular expressions, not being able to end a string
with a backslash is not a bug but a feature.

See http://www.ferg.org/projects/python_...ontents_item_2

(and try not to choke on the oh-so-saccharine-sweet cutesy introduction.
Red Ridinghood indeed *wink*)
--
Steven.

Mar 5 '06 #6
Hi Alex,

thanks for the reply. I can see that there is a choice that needed to
be made. Before I was aware of the \ issue I just used (yes it has
come up, but the example is at work) triple quotes to work around the
embedded quote issue:

x=r'''It's like this "c:\blah\" ok?'''
print x
It's like this "c:\blah\" ok?

Am I missing something?

I guess my point is that it is quite surprising that r'\' is not a
legal way to represent a backslash. I look for consistency in the
tools that I work with and this except-for-the-last-character exception
is annoying (I admit it might be a silly use-case but consistency is
consistency).

Again, thanks for taking the time to reply.

Now get back to work on your new Nutshell book :-)

Mar 5 '06 #7
Hi,

thanks for the reply. I was not aware of this in raw strings (and
frankly, don't like it... but who cares about that :-) )

When I needed embedded quotes in a raw string I went the triple quote
route:

a = r'''check \' this'''

which makes more sense to me.

Mar 5 '06 #8
Hi Steven,

thanks for the reply. I was/am aware that raw strings are mainly used
for regular expressions (and franky that was I usually use them for).
I was not aware that they still have "special powers" in raw strings
(thanks for the link!).

This one bit me when I was doing some quick and dirty hacking and a 5
minute task ended up being a much longer adventure becuase of this
(Note to self: don't post when annoyed).

Mar 5 '06 #9
<pl****@alumni. caltech.edu> wrote:
thanks for the reply. I can see that there is a choice that needed to
be made. Before I was aware of the \ issue I just used (yes it has
come up, but the example is at work) triple quotes to work around the
embedded quote issue:

x=r'''It's like this "c:\blah\" ok?'''
print x
It's like this "c:\blah\" ok?

Am I missing something?
No, triplequotes are perfectly valid.
I guess my point is that it is quite surprising that r'\' is not a
legal way to represent a backslash. I look for consistency in the
tools that I work with and this except-for-the-last-character exception
is annoying (I admit it might be a silly use-case but consistency is
consistency).
Alas, somebody will now quote Emerson at you, I fear;-). Me, I
appreciate consistency and regularity more than most (without making a
fetish of it), but the rule "a rawstring cannot end with an odd number
of backslashes" is perfectly regular and consistent, so I'm not
perturbed by it (it's NOT about the last character, as r'\\' is just
fine even though its last character is a \ -- it's about the parity of
the length of the terminating sequence of backslashes: even is OK [0 is
of course even;-)] and odd is not).
Again, thanks for taking the time to reply.

Now get back to work on your new Nutshell book :-)


Yep, good point!-)
Alex
Mar 5 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
5781
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT" >>>c = extract(a,b) >>>print c "abcdefghijklmnop"
17
7402
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently interchangeable in all circumstances (as long as they're paired) so there's no overloading to muddy the language. Of course there could be some interesting problems with current code that doesn't make a distinction, but it would be dead easy to fix...
16
2439
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold any arbitrary data, even binary data such as photos and movies.They are of course also good for their traditional role of storing and manipulating text." This view of strings is about a decade out of date with modern programmimg practice. From...
4
10540
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three sets of character strings from the user. Then I want to run through each element and compare the two strings. If they match I print they match... I'm having a bit of trouble with the actual loop through each array using the pointers and comparing...
25
3579
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
6
1763
by: Broeisi | last post by:
Hello, I wrote the tiny progam below just to understand arrays and strings better. I have 2 questions about arrays and strings in C. 1. Why is it that when you want to assign a string to an character array that you must use the strcpy() function?
2
22614
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be read (the file will be written by only this program); file can be either in text or binary (preferably binary as the files may be read repeatedly); the amount and size of strings in the array won't be known until run time (in the example I have it in...
19
3117
by: pkirk25 | last post by:
I wonder if anyone has time to write a small example program based on this data or to critique my own effort? A file called Realm List.html contains the following data: Bladefist-Horde Nordrassil-Horde Draenor-Alliance Nordrassil-Alliance Nordrassil-Neutral Draenor-Horde
95
5430
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ____________________________________ | | | ------------------ | | | BUTTON | | | ...
0
9686
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9540
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10475
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10026
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.