我的python代码:
import xml.etree.ElementTree as ETree
tree = ETree.ElementTree(file="CountryData.xml")
root = tree.getroot()
textfile = open("Output.txt", "w")
print("Population densities:")
for i in root:
density = str(float((i.attrib["areaInSqKm"])/float(i.attrib["population"]))
n = (i.get("countryName"))
b = n + ": " + density + "people/km2 \n"
textfile.write(b)
textfile.close()
给我以下错误:
File "G:\CountryData.py", line 13
n = i.get("countryName"))
^
SyntaxError: invalid syntax
我尝试了多种方法和其他编写方式,但我不知道如何解决这个问题。 Doeas有人看到我的错误吗?
ps,在我添加关于人口密度的位之前,它工作正常
答案 0 :(得分:0)
你错过了这一行的结束语:
str(float((i.attrib["areaInSqKm"])/float(i.attrib["population"]))
应该是:
str(float((i.attrib["areaInSqKm"])/float(i.attrib["population"])))
答案 1 :(得分:0)
你错过了1个角色:)
density = str(float((i.attrib["areaInSqKm"])/float(i.attrib["population"]))
density = str(float((i.attrib["areaInSqKm"])/float(i.attrib["population"])))
答案 2 :(得分:0)
请使用以下行代码(您缺少右边的代码):
density = str(float((i.attrib["areaInSqKm"])/float(i.attrib["population"])))