| | |
Sting manipulation
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 198
Reputation:
Solved Threads: 0
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
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
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.
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.
You can use string slicing if you assume a few things:
python Syntax (Toggle Plain Text)
#!/usr/bin/env python # has old = "AND Category 07|Spec 01|ABC 01 AND Category 07|Spec 02|XYZ 02 \ AND Category 07|Spec 03|PQR 03 " # wants new = "AND Category 07|Spec 01 AND Category 07|Spec 02 AND Category 07|Spec 03" # assuming that 'Spec xx' and what you want to remove does not change in length q = old.split('|S') for x in q: print(x) # test print('-'*30) new2 = "" for x in q: if x.startswith('pec'): # skips the chars from index 6 to 12 x = '|S' + x[:6] + x[13:] print(x) # test new2 += x print('-'*30) print(new2) """ my result --> AND Category 07 pec 01|ABC 01 AND Category 07 pec 02|XYZ 02 AND Category 07 pec 03|PQR 03 ------------------------------ |Spec 01 AND Category 07 |Spec 02 AND Category 07 |Spec 03 ------------------------------ AND Category 07|Spec 01 AND Category 07|Spec 02 AND Category 07|Spec 03 """
Last edited by sneekula; May 21st, 2009 at 11:06 am.
No one died when Clinton lied.
•
•
Join Date: Dec 2006
Posts: 1,008
Reputation:
Solved Threads: 285
Another idea. You can also split on the "AND" and then split on "|" removing the [-1] element. Edit: I hope this isn't homework since it's too late now.
Python Syntax (Toggle Plain Text)
test_input = "AND Category 07|Spec 01|ABC 01 AND Category 07|Spec 02|XYZ 02 AND Category 07|Spec 03|PQR 03 " after_and_split = test_input.split("AND") print after_and_split new_str = "" for str in after_and_split: after_line_split = str.split("|") ## length has to be > 1, otherwise nothing is left after removing -1 if len(after_line_split) > 1: print " after_line_split", after_line_split del after_line_split[-1] new_str += "AND" new_str += "|".join(after_line_split) + " " print new_str AND Category 07|Spec 01 AND Category 07|Spec 02 AND Category 07|Spec 03
Last edited by woooee; May 21st, 2009 at 4:38 pm.
•
•
Join Date: May 2009
Posts: 25
Reputation:
Solved Threads: 8
Seems like a good case for a regular expression
python Syntax (Toggle Plain Text)
import re test_input = "AND Category 07|Spec 01|ABC 01 AND Category 07|Spec 02|XYZ 02 AND Category 07|Spec 03|PQR 03 " test_output = re.sub('\|[A-Z]{3} [0-9]{2}', '', test_input)
![]() |
Similar Threads
- DOM Node Manipulation (JavaScript / DHTML / AJAX)
- Help Please - with String Manipulation (C++)
- string manipulation (C)
- C String Manipulation (C)
- information on received e-mail manipulation (Geeks' Lounge)
- Text File Input and manipulation (C++)
Other Threads in the Python Forum
- Previous Thread: manage exception module lightblue
- Next Thread: Using Py2Exe (Python)
| Thread Tools | Search this Thread |
accessdenied advanced apache application argv array beginner book change command converter countpasswordentry csv curved dan08 def dictionary dynamic edit enter event examples file float format function google gui homework import inches input jaunty java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric obexftp output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pygtk pyopengl python random recursion redirect remote return reverse scrolledtext session simple skinning software sprite statictext string strings syntax terminal text threading time tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable voip wordgame wxpython






