私はPythonとSOの比較的初心者です。情報を抽出する必要がある 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 これはサメについての段落です。」であり、これが私が望むものです。
ただし、本当に必要なのは、文字列ではなくファイルから読み取ることができることです。だから私はこのコードを試します:
from xml import etree
node = etree.parse('test3.xml')
identifier = node.findtext('identifier')
description = node.findtext('description')
print identifier, description
今、私の結果は「なしなし」です。ファイルを正しく取得していないか、出力に問題があると感じています。これが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>