views:

121

answers:

6

Given a string named line whose raw version has this value:

\rRAWSTRING

how can I detect if it has the escape character \r? What I've tried is:

if repr(line).startswith('\r'):
    blah...

but it doesn't catch it. I also tried find, such as:

if repr(line).find('\r') != -1:
    blah

doesn't work either. What am I missing?

thx!

EDIT:

thanks for all the replies and the corrections re terminolgy and sorry for the confusion.

OK, if i do this

print repr(line)

then what it prints is:

'\rSET ENABLE ACK\n'

(including the single quotes). i have tried all the suggestions, including:

line.startswith(r'\r')
line.startswith('\\r') 

each of which returns False. also tried:

line.find(r'\r')
line.find('\\r')

each of which returns -1

+1  A: 

You can try either:

if repr(line).startswith(r'\r'):

or

if line.startswith('\r'):

The latter is better: it seems like you are using repr only to get at the escaped character.

Ned Batchelder
@Ned, I think you and I are understand the OP's problem differently (not surprisingly, because he uses weird terminology -- e.g., there is no way `\r` can be an escape **character** as he keeps calling it, so I think he means escape **sequence** when he says that). I think his string is `r'\rRAWSTRING'` (why else is he so insistent on the "raw" part?) while you appear to think its `'\rRAWSTRING'` (leaving all the "rawness" references totally mysterious).
Alex Martelli
A: 

It's not entirely clear what you're asking. You speak of a string, but also a "raw version", and your string contains "RAWSTRING", which seems to imply you are talking about raw strings.

None of these are quite the same thing.

If you have an ordinary string with the character represented by '\r' in it, then you can use any ordinary means to match:

>>> "\rastring".find('\r')
0
>>> 

If you defined an actual "raw string", that won't work because what you put in was not the '\r' character, but the two characters '\' and 'r':

>>> r"\rastring".find('\r')
-1
>>>

In this case, or in the case of an ordinary string that happens to have the characters '\' and 'r', and you want to find those two characters, you'll need to search using a raw string:

>>> r"\rastring".find(r'\r')
0
>>> 

Or you can search for the sequence by escaping the backslash itself:

>>> r"\rastring".find('\\r')
0
>>> 
Nicholas Knight
A: 
if '\r' in line:

If that isn't what you mean, tell us PRECISELY what is in line. Do this:

print repr(line)

and copy/past the result into a edit of your question.

Re your subject: backslash is an escape character, "\r" is an escaped character.

John Machin
thanks for all the replies and the corrections re terminolgy and sorry for the confusion. OK, if i do this "print repr(line)" then what it returns is: '\rSET ENABLE ACK\n' (including the single quotes). i have tried all the suggestions above, such as line.startswith(r'\r') which returns False, as does line.startswith('\\r'), as well as line.find(r'\r') and line.find('\\r') (find is returning -1).
mix
Well then it's about time to try suggestions that WORK, like `if '\r' in line:` or `if line.startswith('\r'):`
John Machin
A: 

Simplest:

>>> s = r'\rRAWSTRING'
>>> s.startswith(r'\r')
True

Unless I badly misunderstand what you're saying, you need to look for r'\r' (or, equivalently but perhaps a tad less readably, '\\r'), the escape sequence, not for '\r', the carriage return character itself.

Alex Martelli
+1  A: 

If:

print repr(line)

Returns:

'\rSET ENABLE ACK\n'

Then:

line.find('\r')
line.startswith('\r')
'\r' in line

are what you are looking for. Example:

>>> line = '\rSET ENABLE ACK\n'
>>> print repr(line)
'\rSET ENABLE ACK\n'
>>> line.find('\r')
0
>>> line.startswith('\r')
True
>>> '\r' in line
True

repr() returns a display string. It actually contains the quotes and backslashes you see when you print the line:

>>> print line
SET ENABLE ACK

>>> print repr(line)
'\rSET ENABLE ACK\n'
>>> print len(line)
16
>>> print len(repr(line))
20
Mark Tolonen
+2  A: 

Dude, seems you have tried everything but the simplest thing, lines.startswith('\r'):

>>> line = '\rSET ENABLE ACK\n'
>>> line.startswith('\r')
True
>>> '\rSET ENABLE ACK\n'.startswith('\r')
True

For now, just hold on on using repr(), \r and r'string' and go with the simplest thing to avoid confusion.

Nas Banov
yah, you're right. i found the \r in the first place by printing using repr() so was thinking i needed to use repr() when i searched for it.
mix