使用 Python 获取 xml 中每种类型的描述列表
Get list of description for each type in xml with Python
我有一个 xml 文件,格式如下:
<batch>
<type1 type="application/pdf" file="1234.pdf">
<...></...>
<...></...>
<description>Description 1</description>
<...></...>
<...></...>
</type1>
<type2 type="application/pdf" file="23456.pdf">
<...></...>
<...></...>
<description>Description 1</description>
<...></...>
<...></...>
</type2>
<type1 type="application/pdf" file="1235.pdf">
<...></...>
<...></...>
<description>Description 2</description>
<...></...>
<...></...>
</type1>
</batch>
我想在 xml 中检索该类型描述列表中的 type1、type2 列表。列表结果为 ['{blabla.com}type1', '{blabla.com/2}type2', '{blabla.com/3}type3', '{blabla.com} type4'等]
我试过了:
test = ET.parse("...\index.xml")
type_list = []
for type in test.iter():
type_list.append(type.tag)
type_list = list(set(type_list))
获取 xml 中的所有类型。但是我怎样才能得到每种类型的所有描述呢?
我想要的结果:
type1: Description 1, Description 2
type2: Description 1, ...
名称空间处理不当,但应该有效
import xml.etree.ElementTree as ET
from collections import defaultdict
test = ET.parse("test.xml")
type_list = defaultdict(set)
ns="{blabla.com}"
for type_ in test.iter():
if type_.tag.startswith(ns+'type'):
ttag=type_.tag.split(ns)[1]
descrs = type_.findall(ns+'description')
for descr in descrs:
type_list[ttag].add(descr.text)
print(type_list)
见下文
import xml.etree.ElementTree as ET
from collections import defaultdict
data = defaultdict(list)
xml = '''<batch>
<type1 type="application/pdf" file="1234.pdf">
<description>Description 1</description>
</type1>
<type2 type="application/pdf" file="23456.pdf">
<description>Description 1</description>
</type2>
<type1 type="application/pdf" file="1235.pdf">
<description>Description 2</description>
</type1>
</batch>'''
root = ET.fromstring(xml)
for _type in list(root):
data[_type.tag].append(_type.find('description').text)
print(data)
输出
defaultdict(<class 'list'>, {'type1': ['Description 1', 'Description 2'], 'type2': ['Description 1']})
我有一个 xml 文件,格式如下:
<batch>
<type1 type="application/pdf" file="1234.pdf">
<...></...>
<...></...>
<description>Description 1</description>
<...></...>
<...></...>
</type1>
<type2 type="application/pdf" file="23456.pdf">
<...></...>
<...></...>
<description>Description 1</description>
<...></...>
<...></...>
</type2>
<type1 type="application/pdf" file="1235.pdf">
<...></...>
<...></...>
<description>Description 2</description>
<...></...>
<...></...>
</type1>
</batch>
我想在 xml 中检索该类型描述列表中的 type1、type2 列表。列表结果为 ['{blabla.com}type1', '{blabla.com/2}type2', '{blabla.com/3}type3', '{blabla.com} type4'等] 我试过了:
test = ET.parse("...\index.xml")
type_list = []
for type in test.iter():
type_list.append(type.tag)
type_list = list(set(type_list))
获取 xml 中的所有类型。但是我怎样才能得到每种类型的所有描述呢?
我想要的结果:
type1: Description 1, Description 2
type2: Description 1, ...
名称空间处理不当,但应该有效
import xml.etree.ElementTree as ET
from collections import defaultdict
test = ET.parse("test.xml")
type_list = defaultdict(set)
ns="{blabla.com}"
for type_ in test.iter():
if type_.tag.startswith(ns+'type'):
ttag=type_.tag.split(ns)[1]
descrs = type_.findall(ns+'description')
for descr in descrs:
type_list[ttag].add(descr.text)
print(type_list)
见下文
import xml.etree.ElementTree as ET
from collections import defaultdict
data = defaultdict(list)
xml = '''<batch>
<type1 type="application/pdf" file="1234.pdf">
<description>Description 1</description>
</type1>
<type2 type="application/pdf" file="23456.pdf">
<description>Description 1</description>
</type2>
<type1 type="application/pdf" file="1235.pdf">
<description>Description 2</description>
</type1>
</batch>'''
root = ET.fromstring(xml)
for _type in list(root):
data[_type.tag].append(_type.find('description').text)
print(data)
输出
defaultdict(<class 'list'>, {'type1': ['Description 1', 'Description 2'], 'type2': ['Description 1']})