Reputation: 79
I have a list of values which looks like:
['Carter Copeland Melius Research LLC - Founding Partner, President and Research Analyst of Aerospace & Defense\n', 'Gregory D. Smith The Boeing Company - CFO and Executive VP of Enterprise Operations, Finance & Strategy\n', 'Maurita B. Sutedja The Boeing Company - VP of IR\n']
I want to add a hyphen after the name and this should look like:
['Carter Copeland - Melius Research LLC - Founding Partner, President and Research Analyst of Aerospace & Defense\n', 'Gregory D. Smith - The Boeing Company - CFO and Executive VP of Enterprise Operations, Finance & Strategy\n', 'Maurita B. Sutedja - The Boeing Company - VP of IR\n']
So the logic is that if there is a "." after the second word then the hyphen will be at the end of the third word or else the hyphen would be after the second word.
How do i do this?
Upvotes: 1
Views: 86
Reputation: 86
I did not fully understand the second type of sentence but i think this is waht you mean.
def add_hyphen(string):
words = string.split(" ")
if words[1][-1]==".":
words.insert(3, '-')
else:
words.insert(2, '-')
return ' '.join(words)
txt = ['Carter Copeland Melius Research LLC - Founding Partner, President and Research Analyst of Aerospace & Defense\n', 'Gregory D. Smith The Boeing Company - CFO and Executive VP of Enterprise Operations, Finance & Strategy\n', 'Maurita B. Sutedja The Boeing Company - VP of IR\n']
for k,string in enumerate(txt):
txt[k] = add_hyphen(txt[k])
print(new_txt)
['Carter Melius - Research LLC - Founding Partner, President and Research Analyst of Aerospace & Defense\n', 'Gregory D. Smith - The Boeing Company - CFO and Executive VP of Enterprise Operations, Finance & Strategy\n', 'Maurita B. Sutedja - The Boeing Company - VP of IR\n']
Upvotes: 2
Reputation: 602
Here is a quick regex I made:
(^\w* \w*([^.]|. \w*))
Replace with:
$1 -
You can try it there https://regex101.com/r/JtjMuE/1
Here is auto auto generated code:
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(^\w* \w*([^.]|. \w*))"
test_str = ("Carter Copeland Melius Research LLC - Founding Partner, President and Research Analyst of Aerospace & Defense\n"
"Gregory D. Smith The Boeing Company - CFO and Executive VP of Enterprise Operations, Finance & Strategy\\n")
subst = "$1 - "
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Upvotes: 0
Reputation: 91
names = ['Carter Copeland Melius Research LLC - Founding Partner, President and Research Analyst of Aerospace & Defense',
'Gregory D. Smith The Boeing Company - CFO and Executive VP of Enterprise Operations, Finance & Strategy',
'Maurita B. Sutedja The Boeing Company - VP of IR']
for index, name in enumerate(names):
splitted = name.split(" ")
if "." in splitted[1]:
splitted.insert(3, "-")
else:
splitted.insert(2, "-")
names[index] = " ".join(splitted)
print(names)
This should do the trick. But it only works when all names are following your mentioned schema.
Upvotes: 2