我正在深入定義和調用函數,但我不確定是否掌握了這個概念。我在Python中有一個if/else語句,它循環遍歷包含XML文檔的文件夾。在下面的腳本中,我有一段代碼,必須在每個if xmlfilename ==
條件之後重新鍵入。在Python中定義和調用函數
我在想如果我將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":"
如果你問一個關於Python的問題,我建議把它作爲標記等) –