python - 搜索关键字之间
标签分隔标签
问题描述
我想在以下 xml 文件中搜索关键字。
<?xml version="1.0" encoding="UTF-8"?>
<TEI xml:space="preserve" xmlns="http://www.tei-c.org/ns/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.tei-c.org/ns/1.0 /home/pisenberg/grobid/grobid-0.6.1/grobid-home/schemas/xsd/Grobid.xsd"
xmlns:xlink="http://www.w3.org/1999/xlink">
<text xml:lang="en">
<body>
<div xmlns="http://www.tei-c.org/ns/1.0"><p>text before ref<ref type="bibr" target="#b18">[19,</ref><ref type="bibr" target="#b46">47,</ref><ref type="bibr" target="#b66">67]</ref>text after ref</p></div>
</body>
</text>
</TEI>
我的代码:
from lxml import etree
import os
import csv
from shutil import copyfile
import pandas as pd
teins = {'tei':'http://www.tei-c.org/ns/1.0'} #info on the xml structure
searchterm = "before" #put your search term in lowercase
filepath = "./test.xml"
with open(filepath,'r', encoding='utf8') as file:
try:
tree = etree.parse(file)
root = etree.XML(etree.tostring(tree))
textNode = root.find(".//tei:text",teins)
for elem in textNode.iter():
if elem.text:
if searchterm.lower() in elem.text.lower():
print(elem.text)
except Exception as e: # work on python 3.x
print(str(e))
如果我搜索“之前”,我可以获得结果,它会打印“之前”。但是,如果我搜索“之后”,它不会打印任何内容。
我觉得在标签之后textNode.iter()
无法到达标签内的文本。我想知道有人知道如何解决这个问题吗?<p>
<ref>
任何帮助将不胜感激!
解决方案
推荐阅读
- vue.js - __webpack_public_path__ 在 vuejs
- python - 如何从 zip 中提取 csv 文件并在 python 中将其保存到磁盘?
- json - JsonMappingException:不是地图,不是数组或不是枚举
- excel - 从启用宏的工作簿复制数据
- github - Hygieia Github Collector 没有获取旧的提交
- here-api - HERE 地图显示和地点 API 之间的结果不一致
- c# - 如何在 IIS windows 身份验证 C# 上添加提供程序
- wordpress - 如果任何访问者查看或访问特定页面 - WordPress,如何强制重定向到登录页面?
- c# - CreateResponse() 和 CreateErrorResponse() 有什么区别?
- apple-watch - Apple Watch 中的 Siri 快捷方式集成