我现在有这个字符串,我想把名字提取出来,例如,Garet Hayes,Ronald Allen,等等
Executives
Garet Hayes - Director, Public Relations
Ronald Allen - Chief Executive Officer
Gilbert Danielson - Executive Vice President and Chief Financial Officer
Steven Michaels - President
John Robinson - Executive Vice President and President and Chief Executive Officer, Progressive Finance Holdings LLC
我可以通过以下代码提取名字Garet Hayes:
def partiesExtractor(doc):
executives = []
executives.append(doc[doc.lower().index('executives') + len('executives') + 1 : doc.index(' -')])
return executives
但我觉得应该有一种更有效的方法,即使只取第一个名字,更不用说第二个或其他名字了。我该怎么办
类似于@azro使用列表理解的解决方案:
你们需要把你们的内容分成几行,然后每一行在短划线上分开,并保留第一部分
您也可以使用
regex
使用正则表达式:
相关问题 更多 >
编程相关推荐