Sting manipulation

Thread Solved

Join Date: Feb 2008
Posts: 198
Reputation: dinilkarun is an unknown quantity at this point 
Solved Threads: 0
dinilkarun dinilkarun is offline Offline
Junior Poster

Sting manipulation

 
0
  #1
May 21st, 2009
Hi All,

I have the following string:
"AND Category 07|Spec 01|ABC 01 AND Category 07|Spec 02|XYZ 02 AND Category 07|Spec 03|PQR 03 "
It is stored in a string variable. I want to remove only the "ABC 01", "XYZ 02", "PQR 03", etc from the original string. The new string should look something like this:
"AND Category 07|Spec 01 AND Category 07|Spec 02 AND Category 07|Spec 03" .

Please help in doing this. I tried using split and strip functions.. But I am unable to get the correct output.
Any help would be much appreciated!.

Regards,
Dinil
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Sting manipulation

 
0
  #2
May 21st, 2009
Well it's hard to say without defined rules of what needs to be excluded.

Is it every "|XXX ##" that follows an instance of 'Spec ##' ? If so I would suggest using the re module to come up with a regex. It'll make it super easy to do this quickly, especially if you compile your regex. Here's the documentation, and if you need help generating the regex, post what you have so far and we'll help correct it as you go.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Sting manipulation

 
0
  #3
May 21st, 2009
You can use string slicing if you assume a few things:
  1. #!/usr/bin/env python
  2.  
  3. # has
  4. old = "AND Category 07|Spec 01|ABC 01 AND Category 07|Spec 02|XYZ 02 \
  5. AND Category 07|Spec 03|PQR 03 "
  6. # wants
  7. new = "AND Category 07|Spec 01 AND Category 07|Spec 02 AND Category 07|Spec 03"
  8.  
  9. # assuming that 'Spec xx' and what you want to remove does not change in length
  10.  
  11. q = old.split('|S')
  12.  
  13. for x in q:
  14. print(x) # test
  15.  
  16. print('-'*30)
  17.  
  18.  
  19. new2 = ""
  20. for x in q:
  21. if x.startswith('pec'):
  22. # skips the chars from index 6 to 12
  23. x = '|S' + x[:6] + x[13:]
  24. print(x) # test
  25. new2 += x
  26.  
  27. print('-'*30)
  28.  
  29. print(new2)
  30.  
  31. """
  32. my result -->
  33. AND Category 07
  34. pec 01|ABC 01 AND Category 07
  35. pec 02|XYZ 02 AND Category 07
  36. pec 03|PQR 03
  37. ------------------------------
  38. |Spec 01 AND Category 07
  39. |Spec 02 AND Category 07
  40. |Spec 03
  41. ------------------------------
  42. AND Category 07|Spec 01 AND Category 07|Spec 02 AND Category 07|Spec 03
  43. """
Last edited by sneekula; May 21st, 2009 at 11:06 am.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,008
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 285
woooee woooee is online now Online
Veteran Poster

Re: Sting manipulation

 
0
  #4
May 21st, 2009
Another idea. You can also split on the "AND" and then split on "|" removing the [-1] element.
  1. test_input = "AND Category 07|Spec 01|ABC 01 AND Category 07|Spec 02|XYZ 02 AND Category 07|Spec 03|PQR 03 "
  2. after_and_split = test_input.split("AND")
  3. print after_and_split
  4.  
  5. new_str = ""
  6. for str in after_and_split:
  7. after_line_split = str.split("|")
  8. ## length has to be > 1, otherwise nothing is left after removing -1
  9. if len(after_line_split) > 1:
  10. print " after_line_split", after_line_split
  11. del after_line_split[-1]
  12. new_str += "AND"
  13. new_str += "|".join(after_line_split) + " "
  14. print new_str
  15.  
  16. AND Category 07|Spec 01 AND Category 07|Spec 02 AND Category 07|Spec 03
Edit: I hope this isn't homework since it's too late now.
Last edited by woooee; May 21st, 2009 at 4:38 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 25
Reputation: The_Kernel is an unknown quantity at this point 
Solved Threads: 8
The_Kernel The_Kernel is offline Offline
Light Poster

Re: Sting manipulation

 
1
  #5
May 21st, 2009
Seems like a good case for a regular expression

  1. import re
  2.  
  3. test_input = "AND Category 07|Spec 01|ABC 01 AND Category 07|Spec 02|XYZ 02 AND Category 07|Spec 03|PQR 03 "
  4. test_output = re.sub('\|[A-Z]{3} [0-9]{2}', '', test_input)
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 198
Reputation: dinilkarun is an unknown quantity at this point 
Solved Threads: 0
dinilkarun dinilkarun is offline Offline
Junior Poster

Re: Sting manipulation

 
0
  #6
May 26th, 2009
Thanks all for the great answers. I have solved it sucessfully..
Thanks once again....
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC