text1='If I controlled the internet | Rives '
text2='Life at 30,000 feet | Richard Brandson'
text3='larry brilliant : A surprising idea for "solving" climate change'
import re
text1='If I controlled the internet | Rives '
text2='Life at 30,000 feet | Richard Brandson'
text3='larry brilliant : A surprising idea for "solving" climate change'
text = (text1, text2, text3)
for item in text:
title, speaker = re.split('.[:|].', item)
print('title:', title, ' - Speaker:', speaker)
输出:
title: If I controlled the internet - Speaker: Rives
title: Life at 30,000 feet - Speaker: Richard Brandson
title: larry brilliant - Speaker: A surprising idea for "solving" climate change
您可以使用这个简单的regex
'.[:|].'
输出:
注意最后一个:)
如果您愿意使用纯字符串函数而不是regex:
使用正则表达式
相关问题 更多 >
编程相关推荐