468,294 Members | 1,802 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,294 developers. It's quick & easy.

Slash in strings

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
6 8138
ghostdog74
511 Expert 256MB
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
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
6,596 Expert 4TB
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
2,851 Expert Mod 2GB
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
6,596 Expert 4TB
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
6,596 Expert 4TB
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.

Similar topics

3 posts views Thread by Xaradas | last post: by
7 posts views Thread by steve | last post: by
1 post views Thread by Victor Porton | last post: by
2 posts views Thread by Greg Collins [Microsoft MVP] | last post: by
23 posts views Thread by dkirkdrei | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.