2 回答

慕村9548890
TA貢獻1884條經驗 獲得超4個贊
findall()返回的是一個列表,你需要對其中內容進行一步操作時,例如直接訪問或寫入數據庫的時候,遍厲列表時用一個變量限制一下就行了。
還是我理解錯了你的意思?

慕姐4208626
TA貢獻1852條經驗 獲得超7個贊
import re
p = re.compile(r'\d+')
print p.findall('one1two2three3four4')
print p.findall('one1two2three3four4')[0:2]
### output ###
# ['1', '2', '3', '4']
# ['1', '2']
你可以用切片操作返回來處理findall
返回的結果來達到你的目的
or
import re
count = 0
# The end point number
endpoint = 2
p = re.compile(r'\d+')
for m in p.finditer('one1two2three3four4'):
count += 1
if count > endpoint: break
print m.group()
### output ###
# 1 2
添加回答
舉報
0/150
提交
取消