Package: sdmx
Read SDMX XML files. I’ve only added the features I’ve needed, so this
is far from being a thorough implementation. Contributions welcome.Installationpip install sdmxUsagesdmx.generic_data_message_reader(fileobj, dsd_fileobj=None, lazy=None)Given a file-like object representing the XML of a generic data message,
return a data message reader.sdmx.compact_data_message_reader(fileobj, dsd_fileobj=None, lazy=None)Given a file-like object representing the XML of a compact data message,
return a data message reader.Optional arguments for data message readersdsd_fileobj: the file-like object representing the XML of the
relevant DSD. Only used if the data message does not contain a URL to
the relevant DSD.lazy: set toTrueto read observations lazily to allow
datasets to be read without loading the entire dataset into memory.
Use with caution: lazy reading makes some assumptions about the
structure of the XML (for instance, that series keys always appear
before any observations in that series). These assumptions seem to be
safe on files that I’ve tested, but that doesn’t mean they’re
universally true.Data message readersEach data message reader has the following attributes:datasets(): returns an iterable ofDatasetReaderinstances.
Each instance corresponds to a<DataSet>element.DatasetReaderADatasetReaderhas the following attributes:key_family(): returns theKeyFamilyfor the dataset. This
corresponds to the<KeyFamilyRef>element.series(): returns an iterable ofSeriesinstances. Each
instance corresponds to a<Series>element.KeyFamilyAKeyFamilyhas the following attributes:name(lang): the name of the key family in the languagelang.describe_dimensions(lang): for each dimension of the key family,
find the referenced concept and use its name in the languagelang. Returns a list of strings in the same order as in the
source file.SeriesASerieshas the following attributes:describe_key(lang): the key of a series is a mapping from each
dimension of the dataset to a value. For instance, if the dataset has
a dimension namedCountry, the value for the series might beUnited Kingdom. Returns an ordered dictionary mapping strings to
lists of strings. The items in the dictionary are in the same order
as the dimensions returned fromdescribe_dimensions(). For
instance, if the dataset has a single dimension calledCountry,
the returned value would be{"Country": ["UnitedKingdom"]}. All
ancestors of a value are also described, with ancestors appearing
before descendents. For instance, if the valueUnited Kingdomhas
the parent valueEurope, which has the parent valueWorld,
the returned value would be{"Country": ["World", "Europe", "UnitedKingdom"]}.observations(): returns an iterable ofObservationinstances.
Each instance corresponds to an<Obs>element.ObservationAnObservationhas the following attributes:timevalueExampleThe script below can be used to print out the values contained in a
generic data message. (If you have a compact data message, then usingcompact_data_message_readerinstead ofgeneric_data_message_readershould also work.) Assuming the script
is saved asread-sdmx-values.py, it can be used like so:pythonread-sdmx-values.pypath/to/generic-data-message.xmlpath/to/dsd.xmlimportsysimportsdmxdefmain():dataset_path=sys.argv[1]dsd_path=sys.argv[2]withopen(dataset_path)asdataset_fileobj:withopen(dsd_path)asdsd_fileobj:dataset_reader=sdmx.generic_data_message_reader(fileobj=dataset_fileobj,dsd_fileobj=dsd_fileobj,)_print_values(dataset_reader)def_print_values(dataset_reader):fordatasetindataset_reader.datasets():key_family=dataset.key_family()name=key_family.name(lang="en")printnamedimension_names=key_family.describe_dimensions(lang="en")+["Time","Value"]forseriesindataset.series():row_template=[]key=series.describe_key(lang="en")forkey_name,key_valueinkey.iteritems():row_template.append(key_value)forobservationinseries.observations(lang="en"):row=row_template[:]row.append(observation.time)row.append(observation.value)printzip(dimension_names,row)main()