前提・実現したいこと
・ElementTreeを使って該当のXML文をパースし指定したタグで挟まれたテキストを抽出したいです。
・XMLファイルによってはsequence="1"~"6"まであります。
発生している問題・エラーメッセージ
・XML文中に"="を含むとエラーをはきます。
Traceback (most recent call last): File "C:\Program Files\Python37\lib\xml\etree\ElementPath.py", line 272, in iterfind selector = _cache[cache_key] KeyError: ('.//parties/jp:applicants-agents-article/jp:applicants-agents sequence="1"/applicant sequence="1"/addressbook lang="ja"/name', (('jp', 'http://www.jpo.go.jp'),)) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\T15015\PycharmProjects\PatentApp\patentApp\test_area.py", line 28, in <module> print("11-1.名前1::" + str(elem.findtext('.//parties/jp:applicants-agents-article/jp:applicants-agents sequence="1"/applicant sequence="1"/addressbook lang="ja"/name', namespaces={'jp':'http://www.jpo.go.jp'}))) File "C:\Program Files\Python37\lib\xml\etree\ElementPath.py", line 320, in findtext elem = next(iterfind(elem, path, namespaces)) File "C:\Program Files\Python37\lib\xml\etree\ElementPath.py", line 286, in iterfind selector.append(ops[token[0]](next, token)) KeyError: '='
該当のソースコード
Python
1# -*- coding: utf-8 -*- 2import sys # sysモジュール読み込み 3import glob # globモジュール読み込み 4import os # osモジュール読み込み 5import codecs # codecsモジュールの読み込み 6from xml.etree.ElementTree import * 7# sys.stdout = codecs.getwriter('utf_8')(sys.stdout) 8 9drive = r"C:\Users\test" 10os.chdir(drive) # 読み込み先に移動 11 12x = 'test.xml' 13tree = parse(x) 14elem = tree.getroot() 15print("11-1.名前1::" + str(elem.findtext('.//parties/jp:applicants-agents-article/jp:applicants-agents sequence="1"/applicant sequence="1"/addressbook lang="ja"/name', namespaces={'jp':'http://www.jpo.go.jp'}))) 16print("12-1.住所1::" + str(elem.findtext('.//parties/jp:applicants-agents-article/jp:applicants-agents sequence="1"/applicant sequence="1"/addressbook lang="ja"/address/text', namespaces={'jp':'http://www.jpo.go.jp'})))
該当のXMLファイル
XML
1<?xml-stylesheet type="text/xsl" href="../../../../../XSL/gat-a.xsl"?> 2 3<!DOCTYPE jp-official-gazette PUBLIC "-//JPO//DTD PUBLISHED PATENT/UTILITY MODEL APPLICATION 1.0//EN" "../../../../../DTD/gat-a.dtd"> 4 5<jp-official-gazette kind-of-jp="A" kind-of-st16="A" lang="ja" dtd-version="1.0" country="JP" xmlns:jp="http://www.jpo.go.jp"><bibliographic-data lang="ja" country="JP"> 6 7 <publication-reference> 8 9 <document-id> 10 11 <country>JP</country> 12 13 <doc-number>2017200451</doc-number> 14 15 <kind>公開特許公報(A)</kind> 16 17 <date>20171109</date> 18 19 </document-id> 20 21 </publication-reference> 22 23 <application-reference> 24 25 <document-id> 26 27 <doc-number>2016093031</doc-number> 28 29 <date>20160506</date> 30 31 </document-id> 32 33 </application-reference> 34 35 <invention-title>飲料</invention-title> 36 37 <parties> 38 39 <jp:applicants-agents-article> 40 41 <jp:applicants-agents sequence="1"> 42 43 <applicant sequence="1"> 44 45 <addressbook lang="ja"> 46 47 <name>株式会社パテント</name> 48 49 <registered-number>000135324</registered-number> 50 51 <address> 52 53 <text>東京都</text> 54 55 </address> 56 57 </addressbook> 58 59 </applicant> 60 61 </jp:applicants-agents> 62 63 </jp:applicants-agents-article> 64 65 <inventors> 66 67 <inventor sequence="1"> 68 69 <addressbook> 70 71 <name>山田次郎</name> 72 73 <address> 74 75 <text>千葉県</text> 76 77 </address> 78 79 </addressbook> 80 81 </inventor> 82 83 <inventor sequence="2"> 84 85 <addressbook> 86 87 <name>山田三郎</name> 88 89 <address> 90 91 <text>神奈川県</text> 92 93 </address> 94 95 </addressbook> 96 97 </inventor> 98 99 </inventors> 100 101 </parties> 102 103 <classification-ipc><edition/><main-clsf>A23L 2/00 20060101AFI20171013BHJP </main-clsf><further-clsf>A23L 2/52 20060101ALI20171013BHJP </further-clsf></classification-ipc> 104 105 <classification-national><country>JP</country><main-clsf>A23L2/00 B</main-clsf><further-clsf>A23L2/00 F</further-clsf></classification-national> 106 107 <number-of-claims jp:adopted-law="claim">1</number-of-claims> 108 109 <jp:request-for-examination true-or-false="false"/> 110 111 <jp:filing-form>OL</jp:filing-form> 112 113 <jp:total-pages>6</jp:total-pages> 114 115 <jp:theme-code-info> 116 117 <jp:theme-code>4B117</jp:theme-code> 118 119 </jp:theme-code-info> 120 121 <jp:f-term-info> 122 123 <jp:f-term>4B117LC03</jp:f-term> 124 125 <jp:f-term>4B117LC04</jp:f-term> 126 127 <jp:f-term>4B117LG11</jp:f-term> 128 129 <jp:f-term>4B117LK08</jp:f-term> 130 131 <jp:f-term>4B117LK11</jp:f-term> 132 133 <jp:f-term>4B117LL02</jp:f-term> 134 135 </jp:f-term-info> 136 137 </bibliographic-data><jp:image-of-bibliographic-data><img id="000001" he="140" wi="160" file="2017200451.tif" img-format="tif" img-content="drawing"/></jp:image-of-bibliographic-data><description> 138
欲しい結果
11-1.名前1::株式会社パテント 12-1.住所1::天王洲アイル
試してみたこと
・"="を他文字に置換 → xml.etree.ElementTree.ParseError
・tree = parse(x)の後ろの行で"="を置換 → AttributeError: 'ElementTree' object has no attribute 'replace'
補足情報(FW/ツールのバージョンなど)
バージョン:Python3.7
開発環境:Atom1.31.2
回答1件
あなたの回答
tips
プレビュー