在代码中创建和放置计数器循环以处理结果
Counter loop creation and placement within code to handle results
我有一些 Python 代码可以遍历大型 XML 文件以在逗号分隔的元素中解析出某些结果。
虽然代码提取了结果,但我还需要它来计算结果。
我如何在我当前的代码中编写这个循环,它应该放在哪里?在我的 .split()
函数之后的循环中?之后?
我的代码:
#Working python code
#Import libraries
import webbrowser
from lxml import etree
#Parses the file to get the tree paths
tree = etree.parse('g_d_anime.xml')
root = tree.getroot()
#Starts the XPATH using findall to verify the elements inside the tree are right
#Might not be necessary, but it's not a bad check to have
tree.findall('WorksXML/Work')
#Opens the output file for the results
h_file = open("xml_stats.html","w")
#Start writing the HTML lines
h_file.write("This block shows the producers and genres for this file.")
h_file.write('<br>')
#These loops first split the string, then select the specific value I want to pull out
for Producers in root.iter('Producers'):
p = Producers.text.split(',')
for producer in p:
#if you require only to display only 'Nitroplus', put '==' in place of '!='
if producer == 'Aniplex':
print(p) #This prints all the values in the strings
h_file.write('<li>' + str(producer) + '</li>') #This only writes the selected value
h_file.write('<br>')
for Genres in root.iter('Genres'):
g = Genres.text.split(',')
for genre in g:
#if you require only to display only 'Magic', put '==' in place of '!='
if genre == 'Magic':
print(g) #This prints all the values in the strings
h_file.write('<li>'+ str(genre) + '</li>') #This only writes the selected value
h_file.write('<br>')
#Should the counting loop for the above go here? Or within the above loop?
h_file.close()
webbrowser.open_new_tab("xml_stats.html")
我不确定将计数循环或 .sum() 或计数器行放在哪里,如果那样更容易的话。
结果应显示如下内容:
Aniplex: 7
Magic: 9
该文件有超过 1500 条结构如下的记录:
<WorksXML>
<Work>
<Title>Fullmetal Alchemist: Brotherhood</Title>
<Type>TV</Type>
<Episodes>64</Episodes>
<Status>Finished Airing</Status>
<Start_airing>2009-4-5</Start_airing>
<End_airing>2010-7-4</End_airing>
<Starting_season>Spring</Starting_season>
<Broadcast_time>Sundays at 17:00 (JST)</Broadcast_time>
<Producers>Aniplex,Square Enix,Mainichi Broadcasting System,Studio Moriken</Producers>
<Licensors>Funimation,Aniplex of America</Licensors>
<Studios>Bones</Studios>
<Sources>Manga</Sources>
<Genres>Action,Military,Adventure,Comedy,Drama,Magic,Fantasy,Shounen</Genres>
<Duration>24 min. per ep.</Duration>
<Rating>R</Rating>
<Score>9.25</Score>
<Scored_by>719706</Scored_by>
<Members>1176368</Members>
<Favorites>105387</Favorites>
<Description>"In order for something to be obtained, something of equal value must be lost." Alchemy is bound by this Law of Equivalent Exchange—something the young brothers Edward and Alphonse Elric only realize after attempting human transmutation: the one forbidden act of alchemy. They pay a terrible price for their transgression—Edward loses his left leg, Alphonse his physical body. It is only by the desperate sacrifice of Edward's right arm that he is able to affix Alphonse's soul to a suit of armor. Devastated and alone, it is the hope that they would both eventually return to their original bodies that gives Edward the inspiration to obtain metal limbs called "automail" and become a state alchemist, the Fullmetal Alchemist. Three years of searching later, the brothers seek the Philosopher's Stone, a mythical relic that allows an alchemist to overcome the Law of Equivalent Exchange. Even with military allies Colonel Roy Mustang, Lieutenant Riza Hawkeye, and Lieutenant Colonel Maes Hughes on their side, the brothers find themselves caught up in a nationwide conspiracy that leads them not only to the true nature of the elusive Philosopher's Stone, but their country's murky history as well. In between finding a serial killer and racing against time, Edward and Alphonse must ask themselves if what they are doing will make them human again... or take away their humanity.
</Description>
</Work>
...
</WorksXML>
我知道循环应该类似于:
for pr in producer:
pr = 0
if pr in producer:
producer[pr] = producer[pr] + 1
else:
producer[pr] = 1
如果谁有更好的写法,请分享。
因为你只想计算Aniplex
和Magic
,你应该把它放在if
块中,然后在循环后写入文件:
aniplex_count = 0
magic_count = 0
for Producers in root.iter("Producers"):
p = Producers.text.split(",")
for producer in p:
if producer == "Aniplex":
aniplex_count += 1
h_file.write(
"<li>" + str(producer) + "</li>"
)
h_file.write("<br>")
for Genres in root.iter("Genres"):
g = Genres.text.split(",")
for genre in g:
if genre == "Magic":
magic_count += 1
h_file.write(
"<li>" + str(genre) + "</li>"
)
h_file.write("<br>")
h_file.write(f"<h2>Aniplex: {aniplex_count}</h2>")
h_file.write(f"<h2>Magic: {magic_count}</h2>")
我有一些 Python 代码可以遍历大型 XML 文件以在逗号分隔的元素中解析出某些结果。
虽然代码提取了结果,但我还需要它来计算结果。
我如何在我当前的代码中编写这个循环,它应该放在哪里?在我的 .split()
函数之后的循环中?之后?
我的代码:
#Working python code
#Import libraries
import webbrowser
from lxml import etree
#Parses the file to get the tree paths
tree = etree.parse('g_d_anime.xml')
root = tree.getroot()
#Starts the XPATH using findall to verify the elements inside the tree are right
#Might not be necessary, but it's not a bad check to have
tree.findall('WorksXML/Work')
#Opens the output file for the results
h_file = open("xml_stats.html","w")
#Start writing the HTML lines
h_file.write("This block shows the producers and genres for this file.")
h_file.write('<br>')
#These loops first split the string, then select the specific value I want to pull out
for Producers in root.iter('Producers'):
p = Producers.text.split(',')
for producer in p:
#if you require only to display only 'Nitroplus', put '==' in place of '!='
if producer == 'Aniplex':
print(p) #This prints all the values in the strings
h_file.write('<li>' + str(producer) + '</li>') #This only writes the selected value
h_file.write('<br>')
for Genres in root.iter('Genres'):
g = Genres.text.split(',')
for genre in g:
#if you require only to display only 'Magic', put '==' in place of '!='
if genre == 'Magic':
print(g) #This prints all the values in the strings
h_file.write('<li>'+ str(genre) + '</li>') #This only writes the selected value
h_file.write('<br>')
#Should the counting loop for the above go here? Or within the above loop?
h_file.close()
webbrowser.open_new_tab("xml_stats.html")
我不确定将计数循环或 .sum() 或计数器行放在哪里,如果那样更容易的话。 结果应显示如下内容:
Aniplex: 7
Magic: 9
该文件有超过 1500 条结构如下的记录:
<WorksXML>
<Work>
<Title>Fullmetal Alchemist: Brotherhood</Title>
<Type>TV</Type>
<Episodes>64</Episodes>
<Status>Finished Airing</Status>
<Start_airing>2009-4-5</Start_airing>
<End_airing>2010-7-4</End_airing>
<Starting_season>Spring</Starting_season>
<Broadcast_time>Sundays at 17:00 (JST)</Broadcast_time>
<Producers>Aniplex,Square Enix,Mainichi Broadcasting System,Studio Moriken</Producers>
<Licensors>Funimation,Aniplex of America</Licensors>
<Studios>Bones</Studios>
<Sources>Manga</Sources>
<Genres>Action,Military,Adventure,Comedy,Drama,Magic,Fantasy,Shounen</Genres>
<Duration>24 min. per ep.</Duration>
<Rating>R</Rating>
<Score>9.25</Score>
<Scored_by>719706</Scored_by>
<Members>1176368</Members>
<Favorites>105387</Favorites>
<Description>"In order for something to be obtained, something of equal value must be lost." Alchemy is bound by this Law of Equivalent Exchange—something the young brothers Edward and Alphonse Elric only realize after attempting human transmutation: the one forbidden act of alchemy. They pay a terrible price for their transgression—Edward loses his left leg, Alphonse his physical body. It is only by the desperate sacrifice of Edward's right arm that he is able to affix Alphonse's soul to a suit of armor. Devastated and alone, it is the hope that they would both eventually return to their original bodies that gives Edward the inspiration to obtain metal limbs called "automail" and become a state alchemist, the Fullmetal Alchemist. Three years of searching later, the brothers seek the Philosopher's Stone, a mythical relic that allows an alchemist to overcome the Law of Equivalent Exchange. Even with military allies Colonel Roy Mustang, Lieutenant Riza Hawkeye, and Lieutenant Colonel Maes Hughes on their side, the brothers find themselves caught up in a nationwide conspiracy that leads them not only to the true nature of the elusive Philosopher's Stone, but their country's murky history as well. In between finding a serial killer and racing against time, Edward and Alphonse must ask themselves if what they are doing will make them human again... or take away their humanity.
</Description>
</Work>
...
</WorksXML>
我知道循环应该类似于:
for pr in producer:
pr = 0
if pr in producer:
producer[pr] = producer[pr] + 1
else:
producer[pr] = 1
如果谁有更好的写法,请分享。
因为你只想计算Aniplex
和Magic
,你应该把它放在if
块中,然后在循环后写入文件:
aniplex_count = 0
magic_count = 0
for Producers in root.iter("Producers"):
p = Producers.text.split(",")
for producer in p:
if producer == "Aniplex":
aniplex_count += 1
h_file.write(
"<li>" + str(producer) + "</li>"
)
h_file.write("<br>")
for Genres in root.iter("Genres"):
g = Genres.text.split(",")
for genre in g:
if genre == "Magic":
magic_count += 1
h_file.write(
"<li>" + str(genre) + "</li>"
)
h_file.write("<br>")
h_file.write(f"<h2>Aniplex: {aniplex_count}</h2>")
h_file.write(f"<h2>Magic: {magic_count}</h2>")