ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python读取xml文件

2020-03-03 14:43:10  阅读:310  来源: 互联网

标签:xml 读取 her ssub python she print attrib root


  • 针对下面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)
wanzi_antang 发布了21 篇原创文章 · 获赞 0 · 访问量 801 私信 关注

标签:xml,读取,her,ssub,python,she,print,attrib,root
来源: https://blog.csdn.net/wanzi_antang/article/details/104630143

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有