使用 python 从文档中剥离(XML?)标记
我的文件包含以下格式的科学家姓名 <科学家姓名>; <科学家>abc
我想使用 python 从上面的格式中删除科学家的名字我应该怎么做? 我想使用常规表情但不知道如何使用它......请帮助
I've file which contains name of scientist in following format<scientist_names>
<scientist>abc</scientist>
</scientist_names>
i want to use python to strip out name of scientists from above format How should I do it??
I would like to use regular epressions but don't know how to use it...please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要使用正则表达式!(所有原因都在[此处]中有很好的解释)
使用 xml/ html 解析器,看看 美丽汤。
DO NOT USE REGULAR EXPRESSIONS! (all reasons well explained [here])
Use an xml/html parser, take a look at BeautifulSoup.
这是 XML,您应该使用像
lxml
这样的 XML 解析器,而不是正则表达式(因为 XML 不是一种常规语言)。这是一个例子:
This is XML and you should use a XML parser like
lxml
instead of regular expressions (because XML is not a regular language).Here is an example:
如前所述,这似乎是 xml。在这种情况下,您应该使用 xml 解析器来解析该文档;我推荐 lxml ( http://lxml.de )。
考虑到您的要求,您可能会发现使用 SAX 样式解析比 DOM 样式更方便,因为 SAX 解析只涉及在解析器遇到特定标记时注册处理程序,只要含义一致即可标签的含义不依赖于上下文,并且您有不止一种类型的标签需要处理(这里可能不是这种情况)。
如果您的输入文档格式可能不正确,您可能希望使用 Beautiful Soup: http://www.crummy.com/software/BeautifulSoup/bs3/documentation.html#解析 XML
As noted, this appears to be xml. In that case, you should use an xml parser to parse this document; I recommend lxml ( http://lxml.de ).
Given your requirements, you may find it more convenient to use SAX-style parsing, rather than DOM-style, because SAX parsing simply involves registering handlers when the parser encounters a particular tag, as long as the meaning of a tag is not dependent on context, and you have more than one type of tag to process (which may not be the case here).
In case your input document may be incorrectly formed, you may wish to use Beautiful Soup: http://www.crummy.com/software/BeautifulSoup/bs3/documentation.html#Parsing XML
这是一个应该为您处理 xml 标签的简单示例
如果您发现任何不清楚的地方请告诉我
Here is an simple example that should handle the xml tags for you
If you find anything unclear just let me know