我是Python的新手。我有一个需要从中提取信息的xml文件。我已经为此苦苦挣扎了好几天,但我想我终于找到了能正确提取信息的东西。现在我很难得到正确的输出。这是我的代码:
from xml import etree
node = etree.fromstring('<dataObject><identifier>5e1882d882ec530069d6d29e28944396</identifier><description>This is a paragraph about a shark.</description></dataObject>')
identifier = node.findtext('identifier')
description = node.findtext('description')
print identifier, description
我得到的结果是“5e1882d882ec530069d6d29e28944396这是一段关于鲨鱼的文章”,这就是我想要的。在
然而,我真正需要的是能够从文件而不是字符串中读取。所以我试试这个代码:
^{pr2}$现在我的结果是“没有”。我有一种感觉,我不是把文件弄对了,就是输出出了问题。下面是test3.xml的内容
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response xmlns="http://www.eol.org/transfer/content/0.3" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dwc="http://rs.tdwg.org/dwc/dwcore/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:dwct="http://rs.tdwg.org/dwc/terms/" xsi:schemaLocation="http://www.eol.org/transfer/content/0.3 http://services.eol.org/schema/content_0_3.xsd">
<identifier>5e1882d822ec530069d6d29e28944369</identifier>
<description>This is a paragraph about a shark.</description>
XML文件使用默认命名空间。您需要使用正确的命名空间限定搜索:
元素树匹配正确的元素。在
您还可以给
^{pr2}$.find()
、findall()
和iterfind()
方法显式的名称空间字典。这不是很好的记录:前缀只有在传入的
namespaces
参数中查找。这意味着您可以使用任何名称空间前缀;API将eol:
部分分开,在namespaces
字典中查找相应的名称空间URL,然后将搜索改为查找XPath表达式{http://www.eol.org/transfer/content/0.3}identifier
。在如果您可以切换到^{} library ,情况会更好;该库支持相同的elementtreeapi,但是在元素的
.nsmap
属性中为您收集名称空间。在您是否想过尝试使用BeautifulGroup使用python解析xml:
http://www.crummy.com/software/BeautifulSoup/bs3/documentation.html#Parsing%20XML
有一些好的文档和一个健康的在线群组,所以支持是相当好的
A
相关问题 更多 >
编程相关推荐