Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何读取xml中元素之间的文件_Python_Xml_Elementtree - Fatal编程技术网

Python:如何读取xml中元素之间的文件

Python:如何读取xml中元素之间的文件,python,xml,elementtree,Python,Xml,Elementtree,我想读和之间的字符串 有没有办法在python中使用ElementTree来读取它 我可以知道怎样读标签标题吗 <head> <title>My Podcasts</title> <title2>My_Podcast2</title2> <dateCreated>Sun, 07 Mar 2010 15:53:26 GMT</dateCreated> <dateMo

我想读和之间的字符串 有没有办法在python中使用ElementTree来读取它

我可以知道怎样读标签标题吗

<head>
    <title>My Podcasts</title>
          <title2>My_Podcast2</title2>
    <dateCreated>Sun, 07 Mar 2010 15:53:26 GMT</dateCreated>
    <dateModified>Sun, 07 Mar 2010 15:53:26 GMT</dateModified>
</head>

我的播客
我的播客2
2010年3月7日星期日15:53:26 GMT
2010年3月7日星期日15:53:26 GMT

是。每个ElementTree节点都有一个
tail
属性。
text
属性获取节点内部的文本,
tail
属性获取节点后面的文本

import xml.etree.cElementTree as ET

tree = ET.ElementTree(file='target.xml')

root = tree.getroot()


for child in root:
    print(child.tag, child.text)
输出:

title My Podcasts
dateCreated Sun, 07 Mar 2010 15:53:26 GMT
dateModified Sun, 07 Mar 2010 15:53:26 GMT

谢谢你的回答。这很有帮助。