为什么我的 xml 解析在我的 python 字典中多次存储相同的结果?
Why is my xml parse storing the same result multiple times in my python dictionary?
我正在尝试解析当用户在 python 中输入条件时返回的 xml 结果,以便从外部 api 请求数据。它确实有效,但问题是,returning/printing 多次出现相同的结果。请帮忙!
anime= []
anime_details= []
title= []
pic= []
plot = []
#loop used to iterate over each tag under anime
for tag in anime_info.findall('anime/info'):
#expression used to store tag discriptions
value = tag.attrib['type']
#bool used to check any tag that contained a picture
if value == 'Picture':
#expression used to store the url for the image of the anime
pic = tag.attrib['src']
# anime_details.append(pic)
#bool used to check any tag that contained the title of the anime
if value == 'Main title':
#expression used to print the data that is within the tag eg:name of anime
title = tag.text
# anime_details.append(title)
#bool used to check any tag that contained the plot
if value == "Plot Summary":
#expression used to print the data that is within the tag eg:summerization of plot
plot = tag.text
#expression used to add anime details to the anime_details dictionary
anime_details.append({'title': title, 'picture': pic, 'plot': plot})
# anime.append(anime_details)
for anime_details in anime_details:
print(anime_details['title'])
print(anime_details['picture'])
print(anime_details['plot'])
Output
XML Snip
在您的代码中,索引循环是数组:
for anime_details in anime_details:
您在 anime/info 上循环播放 xml 文件,而不是循环播放动漫,然后在动漫中搜索信息。这意味着每个信息你得到一个 anime_details,所以每个动漫得到多个 anime_details :
…
animes_details = []
# loop on anime nodes
for anime_node in anime_info.findall("anime"):
# set default valued
title = "?"
pic = ""
plot = ""
# loop on info of anime node
for tag in anime_node.findall('info'):
#expression used to store tag discriptions
value = tag.attrib['type']
…
#expression used to add anime details to the anime_details dictionary
animes_details.append({'title': title, 'picture': pic, 'plot': plot})
for anime_details in animes_details:
print(anime_details['title'])
print(anime_details['picture'])
print(anime_details['plot'])
编辑:正如@Drey 所发现的,必须在内部循环之外设置默认值。
我正在尝试解析当用户在 python 中输入条件时返回的 xml 结果,以便从外部 api 请求数据。它确实有效,但问题是,returning/printing 多次出现相同的结果。请帮忙!
anime= []
anime_details= []
title= []
pic= []
plot = []
#loop used to iterate over each tag under anime
for tag in anime_info.findall('anime/info'):
#expression used to store tag discriptions
value = tag.attrib['type']
#bool used to check any tag that contained a picture
if value == 'Picture':
#expression used to store the url for the image of the anime
pic = tag.attrib['src']
# anime_details.append(pic)
#bool used to check any tag that contained the title of the anime
if value == 'Main title':
#expression used to print the data that is within the tag eg:name of anime
title = tag.text
# anime_details.append(title)
#bool used to check any tag that contained the plot
if value == "Plot Summary":
#expression used to print the data that is within the tag eg:summerization of plot
plot = tag.text
#expression used to add anime details to the anime_details dictionary
anime_details.append({'title': title, 'picture': pic, 'plot': plot})
# anime.append(anime_details)
for anime_details in anime_details:
print(anime_details['title'])
print(anime_details['picture'])
print(anime_details['plot'])
Output
XML Snip
在您的代码中,索引循环是数组:
for anime_details in anime_details:
您在 anime/info 上循环播放 xml 文件,而不是循环播放动漫,然后在动漫中搜索信息。这意味着每个信息你得到一个 anime_details,所以每个动漫得到多个 anime_details :
…
animes_details = []
# loop on anime nodes
for anime_node in anime_info.findall("anime"):
# set default valued
title = "?"
pic = ""
plot = ""
# loop on info of anime node
for tag in anime_node.findall('info'):
#expression used to store tag discriptions
value = tag.attrib['type']
…
#expression used to add anime details to the anime_details dictionary
animes_details.append({'title': title, 'picture': pic, 'plot': plot})
for anime_details in animes_details:
print(anime_details['title'])
print(anime_details['picture'])
print(anime_details['plot'])
编辑:正如@Drey 所发现的,必须在内部循环之外设置默认值。