我正在潛心定義和調用函數,但我不确定是否掌握了這個概念。我在Python中有一個if / else語句,它循環周遊包含XML文檔的檔案夾。在下面的腳本中,我有一段代碼,我必須在每個if xmlfilename ==條件後重新鍵入代碼塊。
我在想,如果我将if element.tag ==開始的塊定義為一個函數,我可以在每個條件if xmlfilename ==之後調用它,我想我已經想出了如何定義該函數,但我不知道該怎麼稱呼它在if xmlfilename ==條件出現之後。任何人都可以建議如何做到這一點或我的方式如何定義和使用功能的作品?
if xmlfilename == "Soil":
if element.tag == "timeinfo":
tree = root.find(".//timeinfo")
tree.clear()
if SINGLEDATE == "'Single Date'":
child1 = ET.SubElement(tree, "sngdate")
child2 = ET.SubElement(child1, "caldate")
child3 = ET.SubElement(child1, "time")
if MULTIPLEDATES == "'Multiple Dates'":
parent = ET.SubElement(tree, "mdattim")
for x, y in enumerate(Date2.split(";")):
#print x, y
replaceMD = y.replace('/', '-')
if x == 0:
#print x, y
child1 = ET.SubElement(parent, "sngdate")
child2 = ET.SubElement(child1, "caldate")
child3 = ET.SubElement(child1, "time")
child2.text = replaceMD
child3.text = "unknown"
else:
child1 = ET.SubElement(parent, "sngdate")
child4 = ET.SubElement(child1, "caldate")
child4.text = replaceMD
if xmlfilename == "Tree":
# Do the same thing as above starting at "if element.tag == "timeinfo":"