当前位置:网站首页>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环境,代码分层管理
- sql 截取数据中 ‘.’的前后数据
- How SSL certificate and public IP address affect SEO
- 利用尾巴作为时间序列进行处理来识别鲸鱼
- C++ exception implementation mechanism
- Guest interview: Wang Jian
- SQL filter query duplicate columns
- 会展云技术解读 | 面对突发事故,APP 如何做好崩溃分析与性能监控?
- Python prompt attributeerror or depreciation warning: This module was degraded solution
- 痞子衡嵌入式:RT-UFL - 一个适用全平台i.MXRT的超级下载算法设计
猜你喜欢
随机推荐
推动中国制造升级,汽车装配车间生产流水线3D可视化
Baishan cloud technology is selected as the top 100 Internet enterprises in China in 2020
假如需要一百万个对象
leetcode之最后一个单词的长度
Application of V7 version of lvgl Library
Usage of [:] and [::] in Python
sql 筛选查询重复列
Fear of reconstruction? I'm too late to tell you how to refactor. Now I'm here
将Map中对应的key和value赋值到对象中
Prometheus installation configuration
Hand in hand to teach you to use container service tke cluster audit troubleshooting
The number of more than half of the array is printed by the sword
Win7 + vs2015 + cuda10.2 configuration tensorrt7.0
Problems of input and button (GAP / unequal height / misalignment) and Solutions
11.9
lodash.js源码-flatten
Coding style: SSM environment in MVC mode, code hierarchical management
那么当下的行情投资者该怎么办呢? 现在新的投资风口来了!
Python中[:]与[::]的用法
How SSL certificate and public IP address affect SEO








