2016-11-02 37 views
1

比方說,我們有文字中的列標題存儲形式:如何從文本蟒蛇提取列數據(正則表達式)

{| 
|+ The table's caption 
! scope="col" width="20"style="background-color:#cfcfcf;"align="center" | Column header 1 
! scope="col" width="20"style="background-color:#ff55ff;"align="center" | Column header 2 
! scope="col" | Column header 3 
|- 
! scope="row" | Row header 1 
| Cell 2 || Cell 3 
|- 
! scope="row" | Row header A 
| Cell B 
| Cell C 
|} 

我怎麼能提取所有的列([列標題1列標題2,列標題3])從Python中的文本?

re.findall('*! scope="col" |', text, re.IGNORECASE) 

但它沒有完成這項工作。

https://regex101.com/r/PLKREz/6

我怎麼能做到這一點在Python?

+0

你從網上刮本,或者是給你這個文本使用? –

+0

@Wintro這是從維基百科文章,我的任務是提取表中的列... –

回答

0

你可以在一行的最後|之後的所有子帶scope="col"

import re 

data = """ 
{| 
|+ The table's caption 
! scope="col" width="20"style="background-color:#cfcfcf;"align="center" | Column header 1 
! scope="col" width="20"style="background-color:#ff55ff;"align="center" | Column header 2 
! scope="col" | Column header 3 
|- 
! scope="row" | Row header 1 
| Cell 2 || Cell 3 
|- 
! scope="row" | Row header A 
| Cell B 
| Cell C 
|}""" 

print(re.findall(r'scope="col".*?\| ([^|]+)$', data, re.MULTILINE)) 

打印:

['Column header 1', 'Column header 2', 'Column header 3']