Brightway2/ActivityBrowser: Is there a way to import a Dataset in xml format to my Database?

142 Views Asked by At

I am using the Activity-Browser and I would like to import a local dataset into my database. My file has xml format. I can not figure out how to import this format. Is there a way to do it or can I do it with python code?

My Data is from: https://www.probas.umweltbundesamt.de/php/prozessdetails.php?id={9FB8299D-EEFF-4D3C-B17C-6085B278C01A}#nachoben. The xml file there is the one I would like to import. I already tried with xml.etree.ElementTree to write the data into pandas but it's not working how I expect. What would be the way to write a database with python into bw? How does my database have to look?

My code looks like this:

#import packages 
import xml.etree.ElementTree as ET
import pandas as pd

#parse the xml file
tree = ET.parse('process.xml')
root = tree.getroot()

#initialize an empty list to store data
A = []

#define a recursive function to parse the xml tree
def parse_element(element, item):
    if len(list(element)) == 0:
        item[element.tag] = element.text
    else:
        for child in list(element):
           parse_element(child, item)

#iterate through the xml tree and parse the elements
for child in root:
    item={}
    parse_element(child, item)
    A.append(item)

df = pd.DataFrame(A)
3

There are 3 best solutions below

0
On

I am not aware of any way to import XLM to AB, it's probably easier to read it in Python and write it to the database with BW than you can use it with AB.

If you need help with that please let me know how your data is structured.

0
On

Sorry for the late reply, but anyway you should check this out, i think it will help you achieve what you want to do. https://github.com/massimopizzol/advanced-lca-notebooks/blob/main/Course-material/4-Excel-import.ipynb

either edit the code to work with XML instead of xlsx or convert your data to xlsx and use directly.

0
On

To create a database you can create a python object that follows the data format specified in the documentation here. You can find examples on how to create a database from that python object (example here). In the old notebooks there are also several examples (notebooks starting by IO). You can indeed use xml.etree.ElementTree to parse the xml file, which does not seem to follow any of the schemas for which there are already importers in bw2io. What I would recommend is that you follow the extract - transform - load (ETL) logic of other bw2io importers. Ideally you'd share the importer with the community so others can use those files too.