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