[英]How to extract column data from a text in python (regex)
假设我们有文本,其中列标题存储在表单中:
{|
|+ 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
|}
如何从python中的文本中提取所有列([ 列标题1 , 列标题2 , 列标题3 ])?
re.findall('*! scope="col" |', text, re.IGNORECASE)
但它没有做好这项工作。
https://regex101.com/r/PLKREz/6
我怎么能在Python中做到这一点?
你可以找到最后一个|
之后的所有子串 在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']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.