首页 > 解决方案 > 解析命名空间 xml 生成 AttributeError: 'str' object has no attribute 'text'

问题描述

以下代码导致此错误“AttributeError:'str'对象没有属性'text'”可以告诉我如何解决这个错误。此错误是由 value.text = str(ImageState) 引起的

for ecu_container in root.findall('.//{http://autosar.org/schema/r4.0}ECUC-CONTAINER-VALUE'):
    short_name = ecu_container.find('.//{http://autosar.org/schema/r4.0}SHORT-NAME').text
    if(short_name == component):
        value = ecu_container.find('.//{http://autosar.org/schema/r4.0}VALUE').text
        value.text = str(imageState)

标签: python-3.xxml-parsing

解决方案


我发现了我的问题。命令
值 = ecu_container.find('.//{http://autosar.org/schema/r4.0}VALUE').text 不正确。它应该是
value = ecu_container.find('.//{http://autosar.org/schema/r4.0}VALUE')。

我已将 .text 添加到此行以读取数据,以确保获得正确的数据以进行更新,但在尝试写入数据时从未删除 .text。


推荐阅读