当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- SQL intercepts the data before and after the '.'
- Python提示AttributeError 或者DeprecationWarning: This module was deprecated解决方法
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- 对于程序员,那些既陌生又熟悉的计算机硬件
- 痞子衡嵌入式:RT-UFL - 一个适用全平台i.MXRT的超级下载算法设计
- JT Jingtao project
- Python prompt attributeerror or depreciation warning: This module was degraded solution
- 编码风格:Mvc模式下SSM环境,代码分层管理
- Gets the property value of a column in the list collection object
- 《Python Cookbook 3rd》笔记(2.1):使用多个界定符分割字符串
猜你喜欢
随机推荐
初级工程师如何在职场生存
做个别人家的网页
Operation and design of rights management in ERP
Aikang Guobin denounced Guoxin Securities report as untrue and sent a lawyer's letter
白山云科技入选2020中国互联网企业百强
CUDA_ Shared memory, memory access mechanism, access optimization
会展云技术解读 | 面对突发事故,APP 如何做好崩溃分析与性能监控?
How SSL certificate and public IP address affect SEO
ES6, ES7, es8 Learning Guide
C / C + + Programming Notes: C language development tank war! In memory of our lost little overlord game
Python中[:]与[::]的用法
SRM系统是什么系统?SRM供应商管理系统功能
CUDA常用概念及注意点
Coding style: SSM environment in MVC mode, code hierarchical management
公网IP地址和SSL证书可以提升SEO吗?
SQL case conversion, remove the space before and after
Incomplete Polyfill of proxy
What is the SRM system? SRM supplier management system functions
接缝雕刻算法:一种看似不可能的图像大小调整方法
z-index属性详解