python读取xml文件

  • 针对下面xml样式,提取其中内容
<emotionml category-set="http://www.w3.org/TR/emotion-voc/xml#big6" expressed-through="text" xmlns="http://www.w3.org/2009/10/emotionml">
	<emotion id="0">
		<category name="happiness" value="3"/>
		<clause cause="N" id="1" keywords="N">
			<text>Her love of dirt gave way to an inclination for finery</text>
		</clause>
		<clause cause="N" id="2" keywords="N">
			<text> and she grew clean as she grew smart</text>
		</clause>
		<clause cause="Y" id="3" keywords="Y">
			<text> she had now the pleasure of sometimes hearing her father and mother remark on her personal improvement</text>
			<cause begin="39" id="1" lenth="64">hearing her father and mother remark on her personal improvement</cause>
			<keywords keywords-begin="17" keywords-lenth="8">pleasure</keywords>
		</clause>
		<clause cause="N" id="4" keywords="N">
			<text>&quot;Catherine grows quite a good-looking girl -- she is almost pretty today</text>
		</clause>
		<clause cause="N" id="5" keywords="N">
			<text>&quot; were words which caught her ears now and then</text>
		</clause>
		<clause cause="N" id="6" keywords="N">
			<text> and how welcome were the sounds</text>
		</clause>
		<clause cause="N" id="7" keywords="N">
			<text> To look almost pretty is an acquisition of higher delight to a girl who has been looking plain the first fifteen years of her life than a beauty from her cradle can ever receive.</text>
		</clause>
	</emotion>
	<emotion id="1">
		<category name="happiness" value="3"/>
		<clause cause="N" id="1" keywords="N">
			<text>Her love of dirt gave way to an inclination for finery</text>
		</clause>
		<clause cause="N" id="2" keywords="N">
			<text> and she grew clean as she grew smart</text>
		</clause>
		<clause cause="N" id="3" keywords="N">
			<text> she had now the pleasure of sometimes hearing her father and mother remark on her personal improvement</text>
		</clause>
		<clause cause="N" id="4" keywords="N">
			<text>&quot;Catherine grows quite a good-looking girl -- she is almost pretty today</text>
		</clause>
		<clause cause="N" id="5" keywords="N">
			<text>&quot; were words which caught her ears now and then</text>
		</clause>
		<clause cause="N" id="6" keywords="N">
			<text> and how welcome were the sounds</text>
		</clause>
		<clause cause="Y" id="7" keywords="Y">
			<text> To look almost pretty is an acquisition of higher delight to a girl who has been looking plain the first fifteen years of her life than a beauty from her cradle can ever receive.</text>
			<cause begin="1" id="1" lenth="21">To look almost pretty</cause>
			<keywords keywords-begin="51" keywords-lenth="7">delight</keywords>
		</clause>
	</emotion>
</emotionml>
  • 代码如下:
import os
import xml.etree.ElementTree as ET

p_path = os.path.abspath(os.path.dirname(os.getcwd()))
path = os.path.join(p_path,'threedata/emotion_cause_english_test.xml')

tree = ET.parse(path)
root = tree.getroot()

# print('root-tag:',root.tag,',root-attrib:',root.attrib,',root-text:',root.text)

for child in root:
     print('编号:',child.attrib['id'])
     for index, sub in enumerate(child):
         if index == 0:
             print('hh:', sub.attrib['name'])
             print('hhv:', sub.attrib['value'])
         else:
             print('sub.attrib:',sub.attrib)
             for index, ssub in enumerate(sub):
                 # print('ssub.attrib:', ssub.attrib)
                 if not ssub.attrib:
                     print('content:', ssub.text)
                 elif 'begin' in ssub.attrib:
                     print('att:', ssub.attrib)
                     print('causecontent:', ssub.text)
                 else:
                     print('wprdatt:', ssub.attrib)
                     print('wordtext:', ssub.text)
python读取xml文件python读取xml文件 wanzi_antang 发布了21 篇原创文章 · 获赞 0 · 访问量 801 私信 关注
上一篇:[Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)] -D. Present(异或性质,按位拆


下一篇:半个小时教你写一个装(bi)逼(she)之地图搜租房