Python 如何从XML文件填充命名元组?

Python 如何从XML文件填充命名元组?,python,for-loop,tuples,populate,Python,For Loop,Tuples,Populate,我有点卡住了 我想知道如何从XML文件填充元组 这就是我到目前为止所做的: from xml.dom import minidom class Code: def __init__(self, ErrorCode, Amount): self.ErrorCode = ErrorCode self.Amount = Amount filepath = "D:\V11\Dog" Codes = (Code('txtPLC_ERROR;A: 16', 0

我有点卡住了


我想知道如何从XML文件填充元组

这就是我到目前为止所做的:

from xml.dom import minidom

class Code:
    def __init__(self, ErrorCode, Amount):
        self.ErrorCode = ErrorCode
        self.Amount = Amount


filepath = "D:\V11\Dog"

Codes = (Code('txtPLC_ERROR;A: 16', 0), Code('txtPLC_ERROR;A: 119', 0), Code('txtPLC_ERROR;B: 95', 0))


def readConfig():
    xmldoc = minidom.parse(filepath + '\Config.xml')
    itemlist = xmldoc.getElementsByTagName('item')
#    print(len(itemlist))
#    print(itemlist[0].attributes['name'].value)
    for s in itemlist:
        print("Some Profound text")
        Codes.ErrorCode += s
readConfig()
现在我得到了这个错误:

   File "..\PycharmProjects\ProjectX\Analyze.py", line 32, in readConfig
    Codes.Errorcode += s
AttributeError: 'tuple' object has no attribute 'Errorcode'>

请不要因为我愚蠢而对这个问题进行标记。

code.ErrorCode+=s
很有意义。代码是一个元组,包含类代码的对象。现在,如果您想添加新的代码对象,您需要执行类似于Code+=Code(string,error\u Code)的操作

然而,它们是不可变的。一旦元组被创建,就不能从元组中添加/删除内容。但是,您可以使用列表

#代码是一个列表
代码=[代码('txtPLC_错误;A:16',0),代码('txtPLC_错误;A:119',0),代码('txtPLC_错误;B:95',0)]
def readConfig():
xmldoc=minidom.parse(filepath+'\Config.xml')
itemlist=xmldoc.getElementsByTagName('item')
对于项目列表中的项目:
#将代码对象附加到代码
代码。追加(代码(s,0))
readConfig()

code
是一个元组,包含类
code
的对象
ErrorCode
code
的属性,而不是元组。
code.Errorcode+=s
应该做什么?嗨!它应该在现有的元组中添加一个额外的“ErrorCode”为什么
#在codes中附加一个code对象
?这就是OP想要的吗?他确认了吗?“我想知道应该如何从XML文件填充元组”——我想。