递增一个整数,并在循环中写入输出xml

2024-05-14 01:12:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用元素树创建一个xml文件,除了一件事之外,我几乎已经完成了

这是我的构建代码

for i in range(colcolation):
    column = et.SubElement(metadatarecords, "metadata-record")
    colname = et.SubElement(column, "remote-name")
    colname.text = colName[i] 
    datatype = et.SubElement(column, "remote-type")
    datatype.text = colRtype
    Localname = et.SubElement(column, "local-name")
    Localname.text = colLName[i]
    Parentname = et.SubElement(column, "parent-name")
    Parentname.text = colpname
    Ordinal = et.SubElement(column, "ordinal")
    Ord  = str(i + 1)
    Ordinal.text = Ord

    if  colName[i] == 'Discount': 
        colld = et.SubElement(column, "precision")
        colld.text = precision
        aggred = et.SubElement(column, "aggregation")
        aggred.text = aggregationp
        ltyped = et.SubElement(column, "local-type")
        ltyped.text = localtypep
        print("1")
    else :
        if colName[i] == 'Profit':
            collp = et.SubElement(column, "precision")
            collp.text = precision 
            aggrep = et.SubElement(column, "aggregation")
            aggrep.text = aggregationp
            ltypep = et.SubElement(column, "local-type")
            ltypep.text = localtypep
            print("2")
        else:
            if colName[i] == 'Quantity':
                collq = et.SubElement(column, "precision")
                collq.text = precision
                aggreq = et.SubElement(column, "aggregation")
                aggreq.text = aggregationp
                ltypeq = et.SubElement(column, "local-type")
                ltypeq.text = localtypep
                print("3")
            else:
                collc = et.SubElement(column, "collation")
                collc.text = collation
                aggrec = et.SubElement(column, "aggregation")
                aggrec.text = aggregation
                ltype = et.SubElement(column, "local-type")
                ltype.text = localtype
                print("4")
            
    colnul = et.SubElement(column, "contains-null")
    colnul.text = containsnull
    attributes=et.SubElement(metadatarecords, "attributes")
    attribute=et.SubElement(attributes, "attribute")
    attribute.set('datatype','string')
    attribute.set('name','DebugRemoteType')
    attribute.text='WSTR'

此顺序标记需要动态打印为1、2、3,打印12次,即从1到12自动递增。当我给出类似上述代码时,我收到错误“类型错误:无法序列化1(类型int)

有人能说怎么做吗

输出样本为

      <metadata-record class='column'>
        <remote-name>Category</remote-name>
        <remote-type>130</remote-type>
        <local-name>[Category]</local-name>
        <parent-name>[Custom SQL Query]</parent-name>
        <remote-alias>Category</remote-alias>
        **<ordinal>1</ordinal>**
        <local-type>string</local-type>
        <aggregation>Count</aggregation>
        <contains-null>true</contains-null>
        <collation>LEN_RUS_S2_WO</collation>
        <attributes>
          <attribute datatype='string' name='DebugRemoteType'>&quot;WSTR&quot;</attribute>
        </attributes>
      </metadata-record>
      <metadata-record class='column'>
        <remote-name>City</remote-name>
        <remote-type>130</remote-type>
        <local-name>[City]</local-name>
        <parent-name>[Custom SQL Query]</parent-name>
        <remote-alias>City</remote-alias>
        **<ordinal>2</ordinal>**
        <local-type>string</local-type>
        <aggregation>Count</aggregation>
        <contains-null>true</contains-null>
        <collation>LEN_RUS_S2_WO</collation>
        <attributes>
          <attribute datatype='string' name='DebugRemoteType'>&quot;WSTR&quot;</attribute>
        </attributes>
      </metadata-record>

谢谢, 阿鲁什


Tags: textnameremotelocaltypeattributecolumnrecord