【问题标题】:Parsing namespace xml generates AttributeError: 'str' object has no attribute 'text'解析命名空间 xml 生成 AttributeError: 'str' object has no attribute 'text'
【发布时间】:2021-11-22 05:08:22
【问题描述】:

以下代码导致此错误“AttributeError:'str' object has no attribute '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)

【问题讨论】:

  • value 已经是目标元素的文本属性。删除第一行的.text,应该可以了

标签: python-3.x xml-parsing


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-04-26
    • 2017-05-10
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多