当前位置:网站首页>Python cookbook 3rd note (2.1): using multiple qualifiers to split strings
Python cookbook 3rd note (2.1): using multiple qualifiers to split strings
2020-11-09 23:53:00 【Giant ship】
Use multiple qualifiers to split strings
problem
You need to split a string into multiple fields , But the separator ( And the space around it ) It's not fixed .
solution
string Object's split() Method is only suitable for very simple string segmentation , It doesn't allow multiple separators or indefinite spaces around them . When you need to cut strings more flexibly , Best use re.split() Method :
>>> line = 'asdf fjdk; afed, fjek,asdf, foo'
>>> import re
>>> re.split(r'[;,\s]\s*', line)
['asdf', 'fjdk', 'afed', 'fjek', 'asdf', 'foo']
Discuss
function re.split() It's very practical , Because it allows you to specify multiple regular patterns for the separator . such as , In the example above , The separator can be a comma , Semicolons or spaces , And it's followed by any space . As long as the pattern is found , The entities on either side of the matching separator are returned as elements in the result . The return result is a list of fields , This heel str.split() The return value type is the same .
When you use re.split() Function time , It is important to note whether the regular expression contains a bracket to capture the group . If capture packets are used , Then the matched text will also appear in the result list . such as , Take a look at the results of this code run :
>>> fields = re.split(r'(;j,j\s)\s*', line)
>>> fields
['asdf', ' ', 'fjdk', ';', 'afed', ',', 'fjek', ',', 'asdf', ',', 'foo']
>>>
Getting split characters is also useful in some cases . such as , You may want to keep the split string , Used to reconstruct a new output string later :
>>> values = fields[::2]
>>> delimiters = fields[1::2] + ['']
>>> values
['asdf', 'fjdk', 'afed', 'fjek', 'asdf', 'foo']
>>> delimiters
[' ', ';', ',', ',', ',', '']
>>> # Reform the line using the same delimiters
>>> ''.join(v+d for v,d in zip(values, delimiters))
'asdf fjdk;afed,fjek,asdf,foo'
>>>
If you don't want to keep the split string in the result list , But if you still need to use parentheses to group regular expressions , Make sure your group is a non capture group , Form like (?:...) . such as :
>>> re.split(r'(?:,j;j\s)\s*', line)
['asdf', 'fjdk', 'afed', 'fjek', 'asdf', 'foo']
>>>
版权声明
本文为[Giant ship]所创,转载请带上原文链接,感谢
边栏推荐
- 商品后台系统优化
- 编码风格:Mvc模式下SSM环境,代码分层管理
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- Common concepts and points for attention of CUDA
- Baishan cloud technology is selected as the top 100 Internet enterprises in China in 2020
- Can public IP address and SSL certificate improve SEO?
- 树莓派鼓捣记 - 设置 wifi
- 痞子衡嵌入式:RT-UFL - 一个适用全平台i.MXRT的超级下载算法设计
- Error running app:Default Activity not found 解决方法
- Validation failed for one or more entities. See 'entityvalidationerrors' solution
猜你喜欢
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
实验2
接缝雕刻算法:一种看似不可能的图像大小调整方法
Come and learn! Development Guide for personalized recommendation system (with internet disk link)
SQL filter query duplicate columns
Hengxun Technology: the way to deal with server downtime
Raspberry pie drum set WiFi
Youtube订阅——解决在弹窗内使用Youtube订阅按钮高度显示不全的问题
Coding style: SSM environment in MVC mode, code hierarchical management
LinkedList源码简析
随机推荐
编码风格:Mvc模式下SSM环境,代码分层管理
2020-11-09:谈谈布隆过滤器和布谷鸟过滤器的相同点和不同点?
Mongodb kernel source code implementation, performance tuning, best operation and maintenance practice series command processing module source code implementation 1
Formal class D25
ES6, ES7, es8 Learning Guide
将Map中对应的key和value赋值到对象中
mongodb内核源码实现、性能调优、最佳运维实践系列-command命令处理模块源码实现一
Can't find other people's problem to solve
js label语法跳出多重循环
算法模板整理(一)
Bifrost 位点管理 之 异构中间件实现难点(1)
Coding style: SSM environment in MVC mode, code hierarchical management
Can public IP address and SSL certificate improve SEO?
What is the SRM system? SRM supplier management system functions
leetcode之最后一个单词的长度
C++异常实现机制
函数计算进阶-IP查询工具开发
Guest interview: Wang Jian
Day85: Luffy: shopping cart switching price according to different validity period & shopping cart deletion operation & price settlement & foreplay of order page
Python中[:]与[::]的用法