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

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.

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
"""

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.

Seems like a good case for a regular expression

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)
commented: very good +8

Thanks all for the great answers. I have solved it sucessfully..
Thanks once again....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.