示例中如何按剧集 ID 对系列进行排序 xml
How to sort series by episode IDs in the example xml
如何根据剧集 ID 对下面 xml 中的系列进行排序?
<response>
<movies_added/>
<movies_removed/>
<series_added>
<series_meta_data>
<displayName>Game of Thrones</displayName>
<seriesItems>
<seriesItem>
<episodeName>Lord Snow</episodeName>
<episodeID>S01E03</episodeID>
</seriesItem>
<seriesItem>
<episodeName>Winter Is Coming</episodeName>
<episodeID>S01E01</episodeID>
</seriesItem>
<seriesItem>
<episodeName>The Kingsroad</episodeName>
<episodeID>S01E02</episodeID>
</seriesItem>
</seriesItems>
</series_meta_data>
<series_meta_data>
<displayName>Breaking Bad</displayName>
<seriesItems>
<seriesItem>
<episodeName>Cat's in the Bag...</episodeName>
<episodeID>S01E02</episodeID>
</seriesItem>
<seriesItem>
<episodeName>...And the Bag's in the River</episodeName>
<episodeID>S01E03</episodeID>
</seriesItem>
<seriesItem>
<episodeName>Pilot</episodeName>
<episodeID>S01E01</episodeID>
</seriesItem>
</seriesItems>
</series_meta_data>
</series_added>
<series_removed/>
</response>
预期的输出应该是这样的:
<response>
<movies_added/>
<movies_removed/>
<series_added>
<series_meta_data>
<displayName>Game of Thrones</displayName>
<seriesItems>
<seriesItem>
<episodeName>Winter Is Coming</episodeName>
<episodeID>S01E01</episodeID>
</seriesItem>
<seriesItem>
<episodeName>The Kingsroad</episodeName>
<episodeID>S01E02</episodeID>
</seriesItem>
<seriesItem>
<episodeName>Lord Snow</episodeName>
<episodeID>S01E03</episodeID>
</seriesItem>
</seriesItems>
</series_meta_data>
<series_meta_data>
<displayName>Breaking Bad</displayName>
<seriesItems>
<seriesItem>
<episodeName>Pilot</episodeName>
<episodeID>S01E01</episodeID>
</seriesItem>
<seriesItem>
<episodeName>Cat's in the Bag...</episodeName>
<episodeID>S01E02</episodeID>
</seriesItem>
<seriesItem>
<episodeName>...And the Bag's in the River</episodeName>
<episodeID>S01E03</episodeID>
</seriesItem>
</seriesItems>
</series_meta_data>
</series_added>
<series_removed/>
</response>
编辑:这是我到目前为止尝试过的:
import xml.etree.ElementTree as ET
def get_episode_id(series_container):
series_container.find("episodeID").text
tree = ET.parse("question.xml")
series_containers = tree.findall(".//series_meta_data/seriesItems")
print(series_containers)
for series_container in series_containers:
series_container[:] = sorted(series_container, key=get_episode_id(series_container))
tree.write("new.xml")
并获得:
AttributeError: 'NoneType' object has no attribute 'text'
您的代码有几个问题。排序时用作键的函数需要 return 要比较的属性。您还需要调用可迭代元素的键,而不是可迭代本身。解决这些问题似乎可以生成工作代码
import xml.etree.ElementTree as ET
def get_episode_id(series_container):
return series_container.find("episodeID").text
tree = ET.parse("question.xml")
series_containers = tree.findall(".//series_meta_data/seriesItems")
for series_container in series_containers:
series_container[:] = sorted(series_container, key=get_episode_id)
tree.write("new.xml")
如何根据剧集 ID 对下面 xml 中的系列进行排序?
<response>
<movies_added/>
<movies_removed/>
<series_added>
<series_meta_data>
<displayName>Game of Thrones</displayName>
<seriesItems>
<seriesItem>
<episodeName>Lord Snow</episodeName>
<episodeID>S01E03</episodeID>
</seriesItem>
<seriesItem>
<episodeName>Winter Is Coming</episodeName>
<episodeID>S01E01</episodeID>
</seriesItem>
<seriesItem>
<episodeName>The Kingsroad</episodeName>
<episodeID>S01E02</episodeID>
</seriesItem>
</seriesItems>
</series_meta_data>
<series_meta_data>
<displayName>Breaking Bad</displayName>
<seriesItems>
<seriesItem>
<episodeName>Cat's in the Bag...</episodeName>
<episodeID>S01E02</episodeID>
</seriesItem>
<seriesItem>
<episodeName>...And the Bag's in the River</episodeName>
<episodeID>S01E03</episodeID>
</seriesItem>
<seriesItem>
<episodeName>Pilot</episodeName>
<episodeID>S01E01</episodeID>
</seriesItem>
</seriesItems>
</series_meta_data>
</series_added>
<series_removed/>
</response>
预期的输出应该是这样的:
<response>
<movies_added/>
<movies_removed/>
<series_added>
<series_meta_data>
<displayName>Game of Thrones</displayName>
<seriesItems>
<seriesItem>
<episodeName>Winter Is Coming</episodeName>
<episodeID>S01E01</episodeID>
</seriesItem>
<seriesItem>
<episodeName>The Kingsroad</episodeName>
<episodeID>S01E02</episodeID>
</seriesItem>
<seriesItem>
<episodeName>Lord Snow</episodeName>
<episodeID>S01E03</episodeID>
</seriesItem>
</seriesItems>
</series_meta_data>
<series_meta_data>
<displayName>Breaking Bad</displayName>
<seriesItems>
<seriesItem>
<episodeName>Pilot</episodeName>
<episodeID>S01E01</episodeID>
</seriesItem>
<seriesItem>
<episodeName>Cat's in the Bag...</episodeName>
<episodeID>S01E02</episodeID>
</seriesItem>
<seriesItem>
<episodeName>...And the Bag's in the River</episodeName>
<episodeID>S01E03</episodeID>
</seriesItem>
</seriesItems>
</series_meta_data>
</series_added>
<series_removed/>
</response>
编辑:这是我到目前为止尝试过的:
import xml.etree.ElementTree as ET
def get_episode_id(series_container):
series_container.find("episodeID").text
tree = ET.parse("question.xml")
series_containers = tree.findall(".//series_meta_data/seriesItems")
print(series_containers)
for series_container in series_containers:
series_container[:] = sorted(series_container, key=get_episode_id(series_container))
tree.write("new.xml")
并获得:
AttributeError: 'NoneType' object has no attribute 'text'
您的代码有几个问题。排序时用作键的函数需要 return 要比较的属性。您还需要调用可迭代元素的键,而不是可迭代本身。解决这些问题似乎可以生成工作代码
import xml.etree.ElementTree as ET
def get_episode_id(series_container):
return series_container.find("episodeID").text
tree = ET.parse("question.xml")
series_containers = tree.findall(".//series_meta_data/seriesItems")
for series_container in series_containers:
series_container[:] = sorted(series_container, key=get_episode_id)
tree.write("new.xml")