BaR*_*Rud 3 python regex string split
在python中,我试图从一行中获取数据.线条看起来像:
1. cpasite=5 nsubl=4 cpatypes=3,4,5,6
2. cpasite=6 nsubl=2 cpatypes=7,8
3. cpasite=7 nsubl=4 cpatypes=9,10
4. cpasite=8 nsubl=2 cpatypes=11,12
5. cpasite=9 nsubl=6 cpatypes=13,14,15,16,17,18
Run Code Online (Sandbox Code Playgroud)
我把它作为正则表达式:
pattern=r'(\d+)\. \s* cpasite=(.*)\s* nsubl=(.*)\s* cpatypes=(.*)'
Run Code Online (Sandbox Code Playgroud)
问题是,我需要这些网站(例如3,4,5,6),以便我可以将它们用于我的目的.但考虑到数字不固定,我不能分裂(据我所知).
我如何使用那些cpasites?
为什么不使用你的regex,然后采取第四个被捕获的组:'3,4,5,6'
然后,您可以拆分该字符串,以获取可以单独使用的值列表:
s = '3,4,5,6'
s = map(int, s.split(','))
print s
[3,4,5,6]
>>> print s[2]
5
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
67 次 |
| 最近记录: |