問題描述
我的源代碼是這樣子的
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)