久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 etree 從文件中解析 xml 在讀取字符串時有效

Parse xml from file using etree works when reading string, but not a file(使用 etree 從文件中解析 xml 在讀取字符串時有效,但不是文件)
本文介紹了使用 etree 從文件中解析 xml 在讀取字符串時有效,但不是文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

I am a relative newby to Python and SO. I have an xml file from which I need to extract information. I've been struggling with this for several days, but I think I finally found something that will extract the information properly. Now I'm having troubles getting the right output. Here is my code:

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

The result that I get is "5e1882d882ec530069d6d29e28944396 This is a paragraph about a shark.", which is what I want.

However, what I really need is to be able to read from a file instead of a string. So I try this code:

from xml import etree
node = etree.parse('test3.xml')
identifier = node.findtext('identifier')
description = node.findtext('description')
print identifier, description

Now my result is "None None". I have a feeling I'm either not getting the file in right or something is wrong with the output. Here is the contents of 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>

解決方案

Your XML file uses a default namespace. You need to qualify your searches with the correct namespace:

identifier = node.findtext('{http://www.eol.org/transfer/content/0.3}identifier')

for ElementTree to match the correct elements.

You could also give the .find(), findall() and iterfind() methods an explicit namespace dictionary. This is not documented very well:

namespaces = {'eol': 'http://www.eol.org/transfer/content/0.3'} # add more as needed

root.findall('eol:identifier', namespaces=namespaces)

Prefixes are only looked up in the namespaces parameter you pass in. This means you can use any namespace prefix you like; the API splits off the eol: part, looks up the corresponding namespace URL in the namespaces dictionary, then changes the search to look for the XPath expression {http://www.eol.org/transfer/content/0.3}identifier instead.

If you can switch to the lxml library things are better; that library supports the same ElementTree API, but collects namespaces for you in a .nsmap attribute on elements.

這篇關于使用 etree 從文件中解析 xml 在讀取字符串時有效,但不是文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時出現問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節點)
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應并將元素值存儲在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對象)
主站蜘蛛池模板: 精品视频 免费 | 日韩在线视频一区 | 欧美日韩国产精品激情在线播放 | 国产一区二区在线免费 | 欧美精品一区三区 | 国产一区免费 | 精品少妇一区二区三区日产乱码 | 欧美日韩高清在线一区 | 亚洲精品视频网站在线观看 | 欧美狠狠操 | 亚洲精品乱码久久久久久黑人 | 国产精品国产三级国产aⅴ中文 | 国产精品久久久久久久久久久久 | 亚洲美女网站 | 午夜成人免费视频 | 中文字幕在线看第二 | 国产成人午夜高潮毛片 | 亚洲图片一区二区三区 | 亚洲欧美第一视频 | 亚洲成人播放器 | 日本亚洲欧美 | 日韩精品在线看 | 国产探花在线精品一区二区 | 中文字幕在线观看一区二区 | 欧美成人精品一区二区三区 | 91av国产在线视频 | 亚洲二区视频 | 一级二级三级在线观看 | 精品美女久久久 | 欧美乱人伦视频 | 国产精品亚洲一区二区三区在线 | 日韩精品一区二区三区在线观看 | 一区二区精品 | 国产精品久久久久久久久久不蜜臀 | 99爱免费| 精品国产精品国产偷麻豆 | 亚洲 日本 欧美 中文幕 | 色www精品视频在线观看 | 99在线播放 | 久久中文字幕一区 | 成人欧美一区二区 |