0

I have string like this

s ='Jacky Chan || He is a CEO ||Managing Director and General Partner, B Robotics || San Francisco Bay Area'

How can I filter out "He is a CEO" part based on keyword "CEO" within the string to look like this using python?

output:
'Jacky Chan||Managing Director and General Partner, B Robotics || San Francisco Bay Area'
1
  • Since each part is separated by ||, you can call s.split('||') and that will return a list of the separate parts. Commented Jul 27, 2017 at 23:42

6 Answers 6

3

Looks like you are using "||" as a delimiter between parts of your string, so split your string by that delimiter:

parts = s.split("||")

Then you say "CEO" is a keyword for filtering out a part, so get every part that doesn't have "CEO" in it:

filtered = [part for part in parts if "CEO" not in part]

Finally, put it back together:

output = "||".join(filtered)

Now you get

>>> print output
Jacky Chan ||Managing Director and General Partner, B Robotics || San Francisco Bay Area

I assume the output you posted was incorrectly spaced. If not, well, there's something else to be done here. You'll have to clarify for me.

Sign up to request clarification or add additional context in comments.

Comments

3

There's nothing wrong with the other answers, but for the sake of completeness, here's how you could do it with regular expressions. Assumes that | is only used for delimiting.

import re
s ='Jacky Chan || He is a CEO || Managing Director and General Partner, B Robotics || San Francisco Bay Area'
print(re.sub(r'\|\|[^|]*CEO[^|]*\|\|', '||', s))

You can learn all about regular expressions, including the Python-specific bits, in the documentation.

Comments

2

If we split the string using the delimiter ||, we can inspect each split and only include it in our new string if it does not contain the string 'CEO'.

'||'.join(substr for substr in s.split('||') if 'CEO' not in substr)

Comments

2
s ='Jacky Chan || He is a CEO ||Managing Director and General Partner, B Robotics || San Francisco Bay Area'
y = "||".join([a for a in s.split("||") if not 'CEO' in a])
print(y)

Comments

1

You can look for CEO and strip away everything between the || separators that contains it, e.g.:

s ='Jacky Chan || He is a CEO ||Managing Director and General Partner, B Robotics || ' \
   'San Francisco Bay Area'

index = s.find("CEO")  # or any other string
if index != -1:  # string found
    replaced = s[:s.rfind("||", 0, index)] + s[s.find("||", index):]
    print(replaced)

# Jacky Chan ||Managing Director and General Partner, B Robotics || San Francisco Bay Area

Comments

1

You could split the string on the || and then rejoin it excluding that segment. So:

s ='Jacky Chan || He is a CEO ||Managing Director and General Partner, B Robotics || San Francisco Bay Area'

s = s.split('||')

s = '||'.join(x for x in s if 'CEO' not in x)

And put it all together for a comprehension:

'||'.join(x for x in s.split('||') if 'CEO' not in x)

Here's also a regex version:

re.sub(r'\|\|[A-z\s]*[CEO]+[A-z\s]*\|\|', "||", s)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.