473,244 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,244 software developers and data experts.

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

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

Similar topics

3
by: Xaradas | last post by:
Someone could tell me why preg_replace eat a slash when it do replacement? <? $stringToReplace = "blah blah {nome} blah blah"; $replacement = "Two slash: \\\\"; $stringToReplace =...
7
by: steve | last post by:
Anyone knows a good script that basically delivers functionality similar to slash dot? I use phpnuke, but am thinking if there is something better out there written in php. -- Posted using the...
30
by: Stephen Ferg | last post by:
I have a question that is not directly Python-related. But I thought I'd ask the most erudite group that I know... :-) When did Windows start accepting the forward slash as a path separator...
1
by: Victor Porton | last post by:
Should we end namespace names (http:// URLs) with a slash or not? Arguments?
2
by: Greg Collins [Microsoft MVP] | last post by:
In reviewing my web site statistics, I see that I get 404 errors (often enough to make me ask about it) when users try to click on a link to a secure external site (i.e. an https:// link). I've...
15
by: fredo | last post by:
I have in my page: <base href="http://www.simple-nude.com/"> and later want to direct a visitor to, say, http://www.simple-nude.com/today/hippie-goddess/15b.jpg It seems that the link...
2
by: Tom | last post by:
I've beat my head every way I can think of against this wall. Maybe someone here just knows the answer. I'm testing an upload class on my laptop WAMP server. When I run the test script,...
23
by: dkirkdrei | last post by:
I am having a bit of trouble trying to double up on slashes in a file path. What I am trying to do is very similar to the code below: <? $var =...
2
by: Perl Beginner | last post by:
Hi, Is there a way to find a line in a text file that starts with a forward slash? This is how i find a line in text file that starts with a word and then print that line to another text file: ...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.