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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: PythonXML文件的常用模块有xml.etree.ElementTree和xml.dom.minidom。 1. 使用xml.etree.ElementTree模块读取XML文件: 首先需要导入xml.etree.ElementTree模块,然后使用ElementTree.parse()方法解析XML文件,返回一个ElementTree对象。接着可以使用该对象的getroot()方法获取XML文件的根元素。可以通过遍历根元素及其子元素的方式来获取XML文件中的数据。 示例代码如下: ```python import xml.etree.ElementTree as ET # 读取XML文件 tree = ET.parse('example.xml') # 获取根元素 root = tree.getroot() # 遍历根元素及其子元素 for child in root: print(child.tag, child.attrib) ``` 2. 使用xml.etree.ElementTree模块写入XML文件: 要写入XML文件,可以创建ElementTree对象,然后使用ElementTree.Element()方法创建根元素。通过调用根元素的Element()方法来创建子元素,以及调用子元素的text属性来设置元素的文本内容。最后使用ElementTree.ElementTree()方法将ElementTree对象写入XML文件。 示例代码如下: ```python import xml.etree.ElementTree as ET # 创建根元素 root = ET.Element('root') # 创建子元素 child1 = ET.Element('child1') child1.text = 'Content of child1' root.append(child1) # 创建ElementTree对象 tree = ET.ElementTree(root) # 写入XML文件 tree.write('example.xml') ``` 以上就是PythonXML文件的简单示例。xml.dom.minidom模块与xml.etree.ElementTree模块类似,提供了操作XML文件的方法,但使用方式略有不同。 ### 回答2: 在Python中,我们可以使用`xml`模块来XML文件。 要读取XML文件,我们可以使用`xml.etree.ElementTree`模块的`parse`函数。具体步骤如下: 1. 导入`xml.etree.ElementTree`模块。 2. 使用`parse`函数打开XML文件,并返回一个`ElementTree`对象。 3. 使用`getroot`方法获取XML文件的根元素。 4. 使用`find`、`findall`等方法定位到需要读取的元素,然后使用`text`属性获取其文本内容。 以下是一个读取XML文件的示例代码: ```python import xml.etree.ElementTree as ET # 打开XML文件并解析 tree = ET.parse('example.xml') # 获取根元素 root = tree.getroot() # 定位到需要读取的元素,获取其文本内容 for child in root.findall('book'): title = child.find('title').text author = child.find('author').text year = child.find('year').text print(f'Title: {title}, Author: {author}, Year: {year}') ``` 要写入XML文件,我们可以使用`xml.etree.ElementTree`模块的相关方法来创建XML元素,并使用`ElementTree`对象的`write`函数将元素写入XML文件。 以下是一个写入XML文件的示例代码: ```python import xml.etree.ElementTree as ET # 创建根元素 root = ET.Element('books') # 创建子元素 book1 = ET.SubElement(root, 'book') title1 = ET.SubElement(book1, 'title') title1.text = 'Book Title 1' author1 = ET.SubElement(book1, 'author') author1.text = 'Author 1' year1 = ET.SubElement(book1, 'year') year1.text = '2021' book2 = ET.SubElement(root, 'book') title2 = ET.SubElement(book2, 'title') title2.text = 'Book Title 2' author2 = ET.SubElement(book2, 'author') author2.text = 'Author 2' year2 = ET.SubElement(book2, 'year') year2.text = '2022' # 创建ElementTree对象 tree = ET.ElementTree(root) # 写入XML文件 tree.write('example.xml', encoding='utf-8', xml_declaration=True) ``` 以上是使用PythonXML文件的基本操作,根据具体需求,我们可以进一步学习和使用XML相关的操作方法。 ### 回答3: PythonXML文件可以使用内置的xml模块。首先,我们需要导入xml.etree.ElementTree模块来处理XML数据。 要读取XML文件,我们可以使用ElementTree的parse函数。例如,如果我们有一个名为"example.xml"的XML文件,我们可以这样读取它: ```python import xml.etree.ElementTree as ET tree = ET.parse("example.xml") root = tree.getroot() ``` 这将解析XML文件并将其存储在ElementTree对象中。我们可以使用getroot()方法获取XML的根元素。 要访问XML元素的内容,可以使用标签名称和迭代器的方式。例如,要打印出XML文件中所有元素的标签和文本内容,可以使用以下代码: ```python for element in root.iter(): print(element.tag, element.text) ``` 要写入XML文件,我们可以创建一个新的ElementTree对象,并添加元素和属性。然后,我们可以使用ElementTree的write函数将其写入文件。 以下是一个将数据写入XML文件的示例代码: ```python import xml.etree.ElementTree as ET root = ET.Element("root") child1 = ET.SubElement(root, "child1") child1.text = "这是child1的文本内容" child2 = ET.SubElement(root, "child2") child2.text = "这是child2的文本内容" tree = ET.ElementTree(root) tree.write("output.xml") ``` 这将创建一个名为"output.xml"的新XML文件,并在其中写入我们创建的XML结构。 综上所述,我们可以使用xml.etree.ElementTree模块XML文件读取时使用parse函数解析XML文件并获取根元素,写入时创建ElementTree对象,添加元素和属性,并利用write函数将其写入文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值