python解析xml文件出现 python xml 'NoneType' object has no attribute 'find'

问题描述

我的源代码是这样子的

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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章