python - get only xml data from text file using python
问题描述
I have a text file where I have some XML data and some HTML data. Both start with "<". Now I want to extract only XML data and save it in another file. How can I do it?
File example:
xyz data:
<note>
<to>john</to>
<from>doe</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
xyz data
<bold>xyz</bold>
text
text
text
<bold>xyz</bold>
again XML data
Note: This file is in .txt format.
解决方案
我不会将您的整个输入视为 XML,而是将其视为 HTML 片段。HTML 可以包含非标准元素,所以<note>
etc. 很好。
为方便起见,我建议pyquery
( link ) 处理 HTML。它的工作方式与 jQuery 几乎相同,所以如果您以前使用过它,它应该很熟悉。
这很简单。加载您的数据,将其包装"<html></html>"
,解析,查询。
from pyquery import PyQuery as pq
data = """xyz data:
<note>
<to>john</to>
<from>doe</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
xyz data
<bold>xyz</bold>
text
text
text
<bold>xyz</bold>
again XML data"""
doc = pq(f"<html><body>{data}</body></html>")
note = doc.find("note")
print(note.find("body").text())
打印"Don't forget me this weekend!"
。
推荐阅读
- javascript - 在反应测试渲染器中调用道具回调不会更新钩子
- python - 高斯未定义
- c# - 如何将标签页、列表框加载到标签页以及将复选框加载到具有特定名称和位置的列表框
- mysql - 我的 SQL 尝试使用事务和锁定
- wordpress - Recaptcha 版本 3 不适用于联系表 7
- android - Android PopupMenu 卡顿+双重渲染
- mysql - SQL - 在另一个表中选择不存在的行
- android - 更新到 firebase-messaging:17.5.0 后出现 AutoAnnotation 错误
- .htaccess - htaccess 变量不显示任何内容
- r - 如何在 kableExtra 中保持标题固定