Search code examples
pythonparsexml

python read xml file and convert into csv file


I am trying to convert xml file into csv file. How to read and parse xml file and convert into csv? Is there any package to convert xml into csv.

<services>
    <service>
        <ServiceID>1</ServiceID>
        <ServiceName>eVoting Booth</ServiceName>
    </service>
    <service>
        <ServiceID>2</ServiceID>
        <ServiceName>Justice of the Peace</ServiceName>
    </service>
    <service>
        <ServiceID>3</ServiceID>
        <ServiceName>Library</ServiceName>
    </service>
        <service>
        <ServiceID>4</ServiceID>
        <ServiceName>Customer Service</ServiceName>
    </service>
    <service>
        <ServiceID>5</ServiceID>
        <ServiceName>Migrant Service</ServiceName>
    </service>
</services>

I want result as

ServiceID | ServiceName
1         | Library
2         | Justice of the Peace

Solution

  • something like this could work:

    from lxml import etree
    import pandas as pd
    
    tree = etree.parse("input.xml")
    
    df = pd.DataFrame({
        "ServiceID" : tree.xpath('/services/service/ServiceID/text()'),
        "ServiceName" : tree.xpath('/services/service/ServiceName/text()')
    })
    
    df.to_csv("output.csv", sep="|", index = None)
    

    this produces

    ServiceID|ServiceName
    1|eVoting Booth
    2|Justice of the Peace
    3|Library
    4|Customer Service
    5|Migrant Service