# Try to get gretl bundles (xml files) into Python # is defined in gretl_io.py: gretl_dotdir = "" def gretl_bdlimport(fname, autodot = 1): if autodot: fname = gretl_dotdir + fname import xml.etree.ElementTree as ET # available in which Py versions? if fname[-3:] == ".gz": import gzip myopen = gzip.open else: myopen = open with myopen(fname, encoding='utf-8') as f: root = ET.fromstring(f.read()) assert root.tag == 'gretl-bundle' gbundle = {} # the dict to be returned try: gbundle['gretl-bundle-name'] = root.attrib['name'] except: pass for child in root: if child.tag == 'bundled-item': type = child.attrib['type'] if type in ('matrix','bundle','series','list'): # anything else? print('Warning: skipping ', child.attrib['key']) elif type == 'string': gbundle[child.attrib['key']] = child.text elif type == 'scalar': gbundle[child.attrib['key']] = float(child.text) elif type == 'array': subtype = child[0].attrib['type'] if subtype in ('matrices','bundles','lists'): print('Warning: skipping ', child.attrib['key']) elif subtype == 'strings': gbundle[child.attrib['key']] = [] # list of strings for ele in child[0]: gbundle[child.attrib['key']].append(ele.text) return gbundle