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.
jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
You can use string slicing if you assume a few things:
#!/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
"""
sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
Another idea. You can also split on the "AND" and then split on "|" removing the [-1] element.
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
Edit: I hope this isn't homework since it's too late now.
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714