如何使用Python在多个分隔符上拆分字符串?
问题
您需要将字符串拆分成多个字段,但是分隔符在整个字符串中并不一致。
解
您可以通过多种方式在python中拆分一个或多个分隔符的字符串。最简单的方法是使用该split()方法,但是,它旨在处理简单的情况。
re.split() is more flexible than the normal `split()` method in handling complex string scenarios.
使用re.split()可以为分隔符指定多个模式。如解决方案中所示,分隔符为ahyphen(-)或whitespace()或逗号(,)后跟的值。正则表达式文档可以在这里找到。
每当找到该模式时,整个匹配都将成为匹配两侧的字段之间的分隔符。
仅提取定界符之间的文本(不定界符)。
示例
import re tennis_greats = 'Roger-federer, Rafael nadal, Novak Djokovic,Andy murray' """" #------------------------------------------------------------------------------------------- # Scenario 1 - Output the players # Input - String with multiple delimiters ( - , white space) # Code - Specify the delimters in [] #------------------------------------------------------------------------------------------- """ players = re.split(r'[-,\s]\s*',tennis_greats)
输出
print(f" The output is - {players}")输出为-[“Roger”,“federer”,“Rafael”,“nadal”,“Novak”,“Djokovic”,“Andy”,“Murray”]
提取定界符之间的文本以及定界符
示例
import re tennis_greats = 'Roger-federer, Rafael nadal, Novak Djokovic,Andy murray' """" #------------------------------------------------------------------------------------------- # Scenario 2 - Output the players and the delimiters # Input - String with multiple delimiters ( - , white space) # Code - Specify the delimters between pipe (|) #------------------------------------------------------------------------------------------- """ players = re.split(r'(-|,|\s)\s*',tennis_greats)
输出
print(f" The output is -{players}")输出为-['Roger','-','federer',',','Rafael',','nadal',',','Novak',','Djokovic',',',“安迪”,“”,“墨累”]
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短