标签:print zip port span 解决 字段 逗号 jdk import
re.split()是很有用的,因为可以为分隔符指定多个模式。例如,在上面的解决方案中,分隔符可以是逗号、分号或者是空格符(后面可跟着任意数量的额外空格)。只要找到了对应的模式,无论匹配点的两端是什么字段,整个匹配的结果就成为那个分隔符。最终得到的结果是字段列表,同str.split()得到的结果一样。
1 line = "asdf fjdk; afed, fjek,asdf, foo" 2 import re 3 result = re.split(r‘[;,\s]\s*‘, line) 4 print(result) 5 6 fields = re.split(r‘(;|,|\s)\s‘, line) 7 print(fields) 8 9 values = fields[::2] 10 delimiters = fields[1::2] + [‘‘] 11 print(values) 12 print(delimiters) 13 14 result1 = ‘‘.join(v+d for v,d in zip(values, delimiters)) 15 print(result1)
运行结果:
1 [‘asdf‘, ‘fjdk‘, ‘afed‘, ‘fjek‘, ‘asdf‘, ‘foo‘] 2 [‘asdf fjdk‘, ‘;‘, ‘afed‘, ‘,‘, ‘fjek,asdf‘, ‘,‘, ‘foo‘] 3 [‘asdf fjdk‘, ‘afed‘, ‘fjek,asdf‘, ‘foo‘] 4 [‘;‘, ‘,‘, ‘,‘, ‘‘] 5 asdf fjdk;afed,fjek,asdf,foo
标签:print zip port span 解决 字段 逗号 jdk import
原文地址:https://www.cnblogs.com/leoych/p/13344096.html