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
"""
Last edited by sneekula; May 21st, 2009 at 11:06 am.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
Offline 2,413 posts
since Oct 2006