【发布时间】:2018-07-11 15:50:23
【问题描述】:
我的问题是来自here 的附加问题,但我不打算将答案部分用于附加问题。
如果我有这样的 XML 文件的一部分:
<eligibility>
<criteria>
<textblock>
Inclusion Criteria:
- women undergoing cesarean section for any indication
- literate in german language
Exclusion Criteria:
- history of keloids
- previous transversal suprapubic scars
- known patient hypersensitivity to any of the suture materials used in the protocol
- a medical disorder that could affect wound healing (eg, diabetes mellitus, chronic
corticosteroid use)
</textblock>
</criteria>
<gender>Female</gender>
<minimum_age>18 Years</minimum_age>
<maximum_age>45 Years</maximum_age>
<healthy_volunteers>No</healthy_volunteers>
</eligibility>
我想提取此资格部分中的所有字符串(即文本块部分和性别、最小年龄、最大年龄和健康志愿者部分中的字符串)
使用上面的代码我做到了:
import sys
from bs4 import BeautifulSoup
soup = BeautifulSoup(open(sys.argv[1], 'r'), 'lxml')
eligibi = []
for eligibility in soup.find_all('eligibility'):
d = {'other_name':eligibility.criteria.textblock.string, 'gender':eligibility.gender.string}
eligibi.append(d)
print eligibi
我的问题是我有很多文件。有时 XML 文件的结构可能是:
eligibility -> criteria -> textblock -> text
eligibility -> other things (e.g. gender as above) -> text
eligibility -> text
例如 如果有办法只取'取所有子标题及其文本'
所以在上面的例子中,列表/字典将包含: {标准文本块:纳入和排除标准,性别:xxx,minimum_age:xxx,maximum_age:xxx,health_volunteers:xxx}
我的问题是,实际上,我不会知道资格标签的所有特定子标签,因为每个实验都可能不同(例如,可能有人说“接受孕妇”、“接受 XXX 的药物史” '等)
所以我只想,如果我给它一个标签名称,它会给我字典中的所有子标签和这些子标签的文本。
用于评论的扩展 XML:
<brief_title>Subcutaneous Adaption and Cosmetic Outcome Following Caesarean Delivery</brief_title>
<source>Klinikum Klagenfurt am Wörthersee</source>
...然后是上面的资格 XML 部分。
【问题讨论】: