金鞍少年 2020-02-26 23:18 采纳率: 0%
浏览 634

python XML模块新增节点问题

XML增加节点内容

1、执行代码

head1 = root.find('country')    # 获取第一个year节点
body = ET.Element('months')     # 创建一个months节点
body.attrib = {"name":"12"}     # 设置属性
body.text = '24day'             # 新增数据
head1.append(body)              # 通过append方法添加节点


2、xml文件

<data>
    <country name="Liechtenstein">
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E" />
        <neighbor name="Switzerland" direction="W" />
    </country>
    <data>

问题:

问题:

在year标签下写入节点就报错

'NoneType' object has no attribute 'append'

在country节点下接入节点正常,这是什么情况呢

  • 写回答

1条回答 默认 最新

  • 偶尔摸点鱼 2023-12-04 08:27
    关注
    评论

报告相同问题?