• python csv文件转换成xml, 构建新xml文件


    csv文件

    code

    from xml.etree.ElementTree import Element,ElementTree,tostring
    import json,csv
    
    def csvtoxml(fname):
        with open(fname,'r') as f:
            reader=csv.reader(f)
            header=next(reader)
            root=Element('Daaa')
            print('root',len(root))
            for row in reader:
                erow=Element('Row')
                root.append(erow)
                for tag,text in zip(header,row):
                    e=Element(tag)
                    e.text=text
                    erow.append(e)
        beatau(root)
        return ElementTree(root)
    
    
    
    def beatau(e,level=0):
        if len(e)>0:
            e.text='
    '+'	'*(level+1)
            for child in e:
               beatau(child,level+1)
            child.tail=child.tail[:-1]
        e.tail='
    ' + '	'*level
        
    et=csvtoxml(r'C:Tempff.csv')
    et.write(r'C:Tempfff.xml')
    

    out

    <Daaa>
                    <Row>
                                    <Summary>summary1</Summary>
                                    <IssueType>2</IssueType>
                                    <Status>do do</Status>
                                    <Priority>hi</Priority>
                                    <Assignee>ui1</Assignee>
                                    <Components>11</Components>
                    </Row>
                    <Row>
                                    <Summary>summary2</Summary>
                                    <IssueType>3</IssueType>
                                    <Status>do do2</Status>
                                    <Priority>hi2</Priority>
                                    <Assignee>ui2</Assignee>
                                    <Components>12</Components>
                    </Row>
                    <Row>
                                    <Summary>summary2</Summary>
                                    <IssueType>4</IssueType>
                                    <Status>do do2</Status>
                                    <Priority>hi2</Priority>
                                    <Assignee>ui2</Assignee>
                                    <Components>13</Components>
                    </Row>
                    <Row>
                                    <Summary>summary4</Summary>
                                    <IssueType>5</IssueType>
                                    <Status>do do4</Status>
                                    <Priority>hi</Priority>
                                    <Assignee>ui4</Assignee>
                                    <Components>14</Components>
                    </Row>
                    <Row>
                                    <Summary>summary5</Summary>
                                    <IssueType>6</IssueType>
                                    <Status>do do5</Status>
                                    <Priority>hi2</Priority>
                                    <Assignee>ui5</Assignee>
                                    <Components>15</Components>
                    </Row>
                    <Row>
                                    <Summary>summary6</Summary>
                                    <IssueType>7</IssueType>
                                    <Status>do do6</Status>
                                    <Priority>hi2</Priority>
                                    <Assignee>ui6</Assignee>
                                    <Components>16</Components>
                    </Row>
                    <Row>
                                    <Summary>Summary</Summary>
                                    <IssueType>8</IssueType>
                                    <Status>Status</Status>
                                    <Priority>Priority</Priority>
                                    <Assignee>Assignee</Assignee>
                                    <Components>17</Components>
                    </Row>
                    <Row>
                                    <Summary>summary7</Summary>
                                    <IssueType>9</IssueType>
                                    <Status>do do7</Status>
                                    <Priority>hi7</Priority>
                                    <Assignee>ui7</Assignee>
                                    <Components>18</Components>
                    </Row>
                    <Row>
                                    <Summary>summary8</Summary>
                                    <IssueType>10</IssueType>
                                    <Status>do do8</Status>
                                    <Priority>hi8</Priority>
                                    <Assignee>ui8</Assignee>
                                    <Components>19</Components>
                    </Row>
    </Daaa>
    
  • 相关阅读:
    BZOJ.2916.[POI1997]Monochromatic Triangles(三元环)
    Codeforces.724G.Xor-matic Number of the Graph(线性基)
    BZOJ.3498.[PA2009]Cakes(三元环 枚举)
    BZOJ.3545.[ONTAK2010]Peaks(线段树合并)
    BZOJ.4919.[Lydsy1706月赛]大根堆(线段树合并/启发式合并)
    BZOJ.2212.[POI2011]Tree Rotations(线段树合并)
    BZOJ.4552.[HEOI2016/TJOI2016]排序(线段树合并/二分 线段树)
    Codeforces.547C.Mike and Foam(容斥/莫比乌斯反演)
    BZOJ.4516.[SCOI2016]幸运数字(线性基 点分治)
    页面置换算法
  • 原文地址:https://www.cnblogs.com/baxianhua/p/10478514.html
Copyright © 2020-2023  润新知