我正在潜心定义和调用函数,但我不确定是否掌握了这个概念。我在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":"