Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用于从wiki模板标记中提取字段的正则表达式_Python_Regex_Python 2.7_Mediawiki_Wikipedia - Fatal编程技术网

Python 用于从wiki模板标记中提取字段的正则表达式

Python 用于从wiki模板标记中提取字段的正则表达式,python,regex,python-2.7,mediawiki,wikipedia,Python,Regex,Python 2.7,Mediawiki,Wikipedia,我想使用Python提取MediaWiki标记中格式为特定字符串的内容。例如,包含名为“被提名人1”和“被提名人2”的字段。玩具示例: In [1]: markup = get_wikipedia_markup('United States presidential election, 2012') In [2]: markup Out[2]: u"{{ | nominee1 = '''[[Barack Obama]]'''\n | party1 = Democratic Party (Unit

我想使用Python提取MediaWiki标记中格式为特定字符串的内容。例如,包含名为“被提名人1”和“被提名人2”的字段。玩具示例:

In [1]: markup = get_wikipedia_markup('United States presidential election, 2012')
In [2]: markup
Out[2]:
u"{{
| nominee1 = '''[[Barack Obama]]'''\n
| party1 = Democratic Party (United States)\n
| home_state1 = [[Illinois]]\n
| running_mate1 = '''[[Joe Biden]]'''\n
| nominee2 = [[Mitt Romney]]\n
| party2 = Republican Party (United States)\n
| home_state2 = [[Massachusetts]]\n
| running_mate2 = [[Paul Ryan]]\n
}}"
以上面的选举文章为例,我想提取紧跟在“被提名人”字段之后但在调用下一个字段(用pip“|”)之前存在的信息。因此,考虑到上面的例子,我最理想的情况是提取“巴拉克·奥巴马”和“米特·罗姆尼”——或者至少提取它们嵌入的语法(“巴拉克·奥巴马”和[[米特·罗姆尼]”)。其他正则表达式也有,但我(失败)尝试使用的是:

nominees = re.findall(r'(?<=\|nominee\d\=)\S+',markup)

namedes=re.findall(r'(?Lookbehinds在这里是不必要的,使用匹配组来指定应该从字符串中提取的内容要容易得多。(事实上,Lookbehinds在这里不能与Python的正则表达式引擎一起工作,因为可选空格使表达式变宽。)

试试这个正则表达式:

\|\s*nominee\d+\s*=\s*(?:''')?\[\[([^]]+)\]\](?:''')?
结果:

re.findall(r"\|\s*nominee\d+\s*=\s*(?:''')?\[\[([^]]+)\]\](?:''')?", markup)
# => ['Barack Obama', 'Mitt Romney']

首先,您在
被提名人\d
之后缺少一个空格。您可能需要
被提名人\d\s*\=
。此外,您确实不想使用正则表达式解析标记。请尝试使用其中一个建议

如果必须使用正则表达式,为什么不使用可读性稍高的多行解决方案呢

import re

markup_string = """{{
| nominee1 = '''[[Barack Obama]]'''
| party1 = Democratic Party (United States)
| home_state1 = [[Illinois]]
| running_mate1 = '''[[Joe Biden]]'''
| nominee2 = [[Mitt Romney]]
| party2 = Republican Party (United States)
| home_state2 = [[Massachusetts]]
| running_mate2 = [[Paul Ryan]]<br>
}}"""

for match in re.finditer(r'(nominee\d\s*\=)[^|]*', markup_string, re.S):
    end_nominee, end_line = match.end(1), match.end(0)
    print end_nominee, end_line
    print markup_string[end_nominee:end_line]
重新导入
markup_string=“”{{
|提名人1=''[[Barack Obama]]''
|party1=民主党(美国)
|母国1=[[伊利诺伊州]]
|跑步1=''[[Joe Biden]]''
|提名人2=[[米特·罗姆尼]]
|party2=共和党(美国)
|母国2=[[马萨诸塞州]]
|跑步2=[[Paul Ryan]]
}}""" 对于re.finditer(r'(被提名者\d\s*\=)[^ |]*',标记字符串,re.s)中的匹配: 结束被提名人,结束行=匹配。结束(1),匹配。结束(0) 打印结束\提名人,结束\行 打印标记\u字符串[结束\u指定人:结束\u行]
对于这样的信息盒数据,最好使用DBpedia。他们已经为您完成了所有提取工作:)

请参阅“Ontology Infobox Properties”文件。您不必是这里的Ontology专家。只需使用简单的tsv解析器来查找您需要的信息!

使用!它压缩了您的代码,并且更容易捕获结果。对于此示例的使用:

import mwparserfromhell as mw
text = get_wikipedia_markup('United States presidential election, 2012')
code = mw.parse(text)
templates = code.filter_templates()
for template in templates:
    if template.name == 'Infobox election':
        nominee1 = template.get('nominee1').value
        nominee2 = template.get('nominee2').value
print nominee1
print nominee2

捕获结果非常简单。

顺便说一句,现在你应该使用Wikidata,它将为你提供已经很好地解析和结构化的Wikipedia数据。