Skip to main content

Convert XML file to a pandas dataframe. This package flattens the XML structure and creates a list of dictionaries that is then transformed to a dataframe.

Project description

xml2df

Convert XML file to a pandas dataframe. This package flattens the XML structure and creates a list of dictionaries that is then transformed to a dataframe.

requirement: pandas

The code is available in the xml2df.py Running the file will allow you to process the example.xml file:

def main(): document = ET.parse("example.xml")

# batched elements is a list containing all the nodes who's children are of the same instance

batched_elements = ["publish_date", "author_details"]

df_result = xml2df(document, "book", batched_elements)
print(df_result.head(5))

The file contains several functions to process your XML file. Please find the descriptions below:

xml2df function:

This function runs the xml_flatten function to supply us with a flat structure result, the result of the xml_flatten function is then
    modified to join all existing lists to strings. If you do not need this final modification, please consider using the xml_flatten
    function instead which takes the same arguments.
xml_document: the xml document to be processed
root_node: specify the root node to begin drilldown
batched_elements: as described in info allows you to identify batched nodes. For batched nodes, the xml.text is requested for all its
    child nodes. If any child nodes have their own children, this function will iter over all children and get their texts as well.
    Please note that when a the children have their own child node, they will ALSO be treated as a regular node and get their own values
    in a separate column.
    example:
        <book>
            <publication_date version="1">
                <year>2020</year>
                <month>05</month>
                <day>11</day>
            </publication_date>
            <publication_date version="2">
                <year>2020</year>
                <month>05</month>
                <day>11</day>
            </publication_date>
            <author>
                John Doe
            </author>
        </book>
        To keep the subinstances of publication date as a whole, "publication_date" must be added to the batched_elements list
        leave empty if you don't need this
unique_results: default set to True, set  to False if you need to get all results and not only unique results (applies set function)
join_separator: default set to ";". modify this if you want the lists to be joined with any other separator

xml_flatten function:

This function, given the root node, will drill down the elements and flatten the xml.
xml_document: the xml document to be processed
root_node: specify the root node to begin drilldown
batched_elements: as described in info allows you to identify batched nodes. For batched nodes, the xml.text is requested for all its
    child nodes. If any child nodes have their own children, this function will iter over all children and get their texts as well.
    Please note that when a the children have their own child node, they will ALSO be treated as a regular node and get their own values
    in a separate column

get_flat_children function:

This function will simply iterate over every node until there are no child nodes found. At this point the function will
    create a dictionary that saves the value of the node text along with the full attribute (name=value).
    format is then {"nodeparent_nodechild": "attribute_name=attribute_value|node_text"}. 
    Please note that only attributes belonging to the leaves (nodes without children) is retrieved. in case of batched 
    elements the attributes of the batch node are retrieved.
batched_elements: please refer to the full explanation in xml2df function.
result_dict: dictionary to aggregates the results.
root: initially, the value supplied will be used to start the drilldown. when this function calls itself, the root will be changed
    to its children.
prev_root: default set to empty string. do not modify unless you want all column names to start with a given string.
attribute_separator: default set to "|". This separator is used to explicitly distinguish attributes from values in the final string.
batch_result_separator: default set to " - ". This value can be modified to change the join string on the batched results.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xml2df-0.12.tar.gz (4.2 kB view details)

Uploaded Source

File details

Details for the file xml2df-0.12.tar.gz.

File metadata

  • Download URL: xml2df-0.12.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.7.5

File hashes

Hashes for xml2df-0.12.tar.gz
Algorithm Hash digest
SHA256 db7e112bfcfd94dba6210ceb1e7e5afd3c9ff45658245a43449cf5b8fbb62404
MD5 5af5f58d60b5d86078284eed550356b2
BLAKE2b-256 972a319e46176e655a021d1af0a06d896d0674e104e77e0ca7af68530b73c7a1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page