943,808 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 568
  • Python RSS
May 21st, 2009
0

Sting manipulation

Expand Post »
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
Similar Threads
Reputation Points: 18
Solved Threads: 0
Posting Whiz in Training
dinilkarun is offline Offline
206 posts
since Feb 2008
May 21st, 2009
0

Re: Sting manipulation

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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
May 21st, 2009
0

Re: Sting manipulation

You can use string slicing if you assume a few things:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
May 21st, 2009
0

Re: Sting manipulation

Another idea. You can also split on the "AND" and then split on "|" removing the [-1] element.
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006
May 21st, 2009
1

Re: Sting manipulation

Seems like a good case for a regular expression

python Syntax (Toggle Plain Text)
  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)
Reputation Points: 43
Solved Threads: 14
Light Poster
The_Kernel is offline Offline
37 posts
since May 2009
May 26th, 2009
0

Re: Sting manipulation

Thanks all for the great answers. I have solved it sucessfully..
Thanks once again....
Reputation Points: 18
Solved Threads: 0
Posting Whiz in Training
dinilkarun is offline Offline
206 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: manage exception module lightblue
Next Thread in Python Forum Timeline: Using Py2Exe (Python)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC