问题描述
我的源代码是这样子的
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
解决方案
在进行size.find('width').text
之前,需要确保size.find('width')
是不为None的。否则就会报错。因此我将代码更改成了这样
size = root.find('size')
w = 1000
h = 600
# 默认大小
if(size is not None):
w = int(size.find('width').text)
h = int(size.find('height').text)
print(w, h)