By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,036 Members | 1,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,036 IT Pros & Developers. It's quick & easy.

Slash in strings

P: 7
Hi to all,

I have to compare two strings in which there is the character \, for example:

"hello\r, bye\n"

but the second string, which is taken from a mysql db, has the following form

"hello\\r, bye \\n"

Obviously the compare returns that the strings are different. How can I unescape the slash in the second string to have a positive match?

I tried to use the replace function to strip one slash, but it doesn't work. I have also tried to make a match using the ASCII codes but \\ is considered as a single character but in the other string \r is considered as a single character (also the lengths are different).

Thanks in advance,
Daniele
Jun 1 '07 #1
Share this Question
Share on Google+
6 Replies


Expert 100+
P: 511
not thoroughly tested for all cases.
Expand|Select|Wrap|Line Numbers
  1. >>> data1=[i.strip("\\r\\n ") for i in "hello\\r, bye \\n".split(",")]
  2. >>> data=[i.strip("\r\n ") for i in "hello\r, bye\n".split(",") ]
  3. >>> data1
  4. ['hello', 'bye']
  5. >>> data
  6. ['hello', 'bye']
  7. >>> data1 == data
  8. True
  9. >>> 
  10.  
Jun 1 '07 #2

P: 7
Uhm... This doesn't work for me because I want to check also for the meta characters \r \n \\ and all which starts with a \ (for example \t). I've tried also re.sub with a regular expression but it doesn't work with the simple \

I succeed in removing the slash for all the characters which follow a \ but not with the \\
Jun 1 '07 #3

bartonc
Expert 5K+
P: 6,596
Hi to all,

I have to compare two strings in which there is the character \, for example:

"hello\r, bye\n"

but the second string, which is taken from a mysql db, has the following form

"hello\\r, bye \\n"

Obviously the compare returns that the strings are different. How can I unescape the slash in the second string to have a positive match?

I tried to use the replace function to strip one slash, but it doesn't work. I have also tried to make a match using the ASCII codes but \\ is considered as a single character but in the other string \r is considered as a single character (also the lengths are different).

Thanks in advance,
Daniele
It looks to me like the strings are being inserted into the DB with quote(s), mySQL function. Is that true? Is that needed? Strings should be able to come and go from mySQL db to python with no problem.
Jun 1 '07 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Uhm... This doesn't work for me because I want to check also for the meta characters \r \n \\ and all which starts with a \ (for example \t). I've tried also re.sub with a regular expression but it doesn't work with the simple \

I succeed in removing the slash for all the characters which follow a \ but not with the \\
Add an additional slash in the first string:
Expand|Select|Wrap|Line Numbers
  1. >>> s1 = "hello\r, bye\n"
  2. >>> s2 = "hello\\r, bye\\n"
  3. >>> print repr(s1).replace("'", "") == s2
  4. True
Remove the extra slash from the second string:
Expand|Select|Wrap|Line Numbers
  1. >>> import re
  2. >>> s2 = "hello\\r, bye\\n"
  3. >>> strList = re.findall(r'(\\[a-z])', s2)
  4. >>> strList
  5. ['\\r', '\\n']
  6. >>> dd = {'\\r': '\r', '\\t': '\t', '\\n': '\n'}
  7. >>> for item in strList:
  8. ...     s2 = s2.replace(item, dd[item])
  9. ...     
  10. >>> s2
  11. 'hello\r, bye\n'
  12. >>> 
Jun 2 '07 #5

bartonc
Expert 5K+
P: 6,596
Add an additional slash in the first string:
Expand|Select|Wrap|Line Numbers
  1. >>> s1 = "hello\r, bye\n"
  2. >>> s2 = "hello\\r, bye\\n"
  3. >>> print repr(s1).replace("'", "") == s2
  4. True
Remove the extra slash from the second string:
Expand|Select|Wrap|Line Numbers
  1. >>> import re
  2. >>> s2 = "hello\\r, bye\\n"
  3. >>> strList = re.findall(r'(\\[a-z])', s2)
  4. >>> strList
  5. ['\\r', '\\n']
  6. >>> dd = {'\\r': '\r', '\\t': '\t', '\\n': '\n'}
  7. >>> for item in strList:
  8. ...     s2 = s2.replace(item, dd[item])
  9. ...     
  10. >>> s2
  11. 'hello\r, bye\n'
  12. >>> 
You are a very clever man, BV. I was playing with the repr() thing, but got distracted (and was working backwards). I like your style. Thanks, buddy.
Jun 2 '07 #6

bartonc
Expert 5K+
P: 6,596
You are a very clever man, BV. I was playing with the repr() thing, but got distracted (and was working backwards). I like your style. Thanks, buddy.
This is how my brain works:
>>> s1 = "hello\r, bye\n"
>>> s2 = "hello\\r, bye\\n"
>>> print repr(s1)[1:-1] == s2
True
>>>
Jun 2 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.