Standard formats¶
The SDMX Information Model provides terms and concepts for data and metadata, but does not specify how that (meta)data is stored, represented, or serialized. Other parts of the SDMX standard describe formats for storing data, metadata, and structures.
sdmx.format captures information about these formats,
including their versions and options/parameters.
This information is used across other modules including sdmx.reader,
sdmx.client, and sdmx.writer.
In general, the sdmx package:
reads most SDMX-CSV, SDMX-JSON 1.0, and SDMX-ML messages; see details in the individual sections below and the linked
readersubmodules.writes certain SDMX-CSV and SDMX-ML formats; see details below and the linked .writer submodules.
is tested using collected specimens of messages in various formats, stored in the khaeru/sdmx-test-data Git repository. These are used to check that the code functions as intended, but can also be viewed to understand the data formats.
SDMX media types. |
|
|
Flag values for information about |
|
Structure of elements in |
|
Return the string for each item in |
- class sdmx.format.Flag(*values)[source]¶
Bases:
IntFlagFlag values for information about
MediaType:data:Trueif this format contains (meta)data.Falseif it contains (meta)data structures.meta:Trueif this format contains metadata (or metadata structures).Falseotherwise.ss:Trueif this format contains data that is structure-specific. This distinction is only relevant before SDMX 3.0.ts:Trueif this format contains time-series data. This distinction is only relevant before SDMX 3.0.
- sdmx.format.MEDIA_TYPES = [application/vnd.sdmx.generic+xml; version=2.1, application/vnd.sdmx.genericdata+xml; version=2.1, application/vnd.sdmx.genericmetadata+xml; version=2.1, application/vnd.sdmx.generictimeseriesdata+xml; version=2.1, application/vnd.sdmx.schema+xml; version=2.1, application/vnd.sdmx.structure+xml; version=2.1, application/vnd.sdmx.structurespecificdata+xml; version=2.1, application/vnd.sdmx.structurespecificmetadata+xml; version=2.1, application/vnd.sdmx.structurespecifictimeseriesdata+xml; version=2.1, application/xml; version=2.1, text/xml; version=2.1, application/vnd.sdmx.data+xml; version=3.0.0, application/vnd.sdmx.structure+xml; version=3.0.0, application/vnd.sdmx.metadata+xml; version=2.0.0, application/vnd.sdmx.data+json; version=1.0.0, application/vnd.sdmx.data+json; version=2.0.0, application/vnd.sdmx.structure+json; version=1.0.0, application/vnd.sdmx.structure+json; version=2.0.0, application/vnd.sdmx.metadata+json; version=2.0.0, application/vnd.sdmx.draft-sdmx-json+json; version=1.0.0, draft-sdmx-json; version=1.0.0, text/json; version=1.0.0, application/vnd.sdmx.data+csv; version=1.0.0, application/vnd.sdmx.metadata+csv; version=2.0.0][source]¶
SDMX media types. Each record is an instance of
MediaType.
- class sdmx.format.MediaType(label: str, base: ~typing.Literal['csv', 'json', 'xml'], _version: dataclasses.InitVar[str | sdmx.format.Version], flags: ~sdmx.format.Flag = <Flag: 0>, full: str | None = None)[source]¶
Bases:
objectStructure of elements in
MEDIA_TYPES.The
str()of a MediaType is generally of the form:application/vnd.sdmx.{label}+{base};version={version}…unless
fullis provided, in which case label and base are ignored.
- sdmx.format.list_media_types(**filters) list[MediaType][source]¶
Return the string for each item in
MEDIA_TYPESmatching filters.
SDMX-CSV¶
Reference: https://github.com/sdmx-twg/sdmx-csv; see in particular the file sdmx-csv-field-guide.md.
Based on Comma-Separated Value (CSV). The SDMX-CSV format is versioned differently from the overall SDMX standard:
SDMX-CSV 1.0 corresponds to SDMX 2.1. It supports only data and metadata, not structures. SDMX-CSV 1.0 files are recognizable by the header
DATAFLOWin the first column of the first row.Added in version 2.9.0: Support for writing SDMX-CSV 1.0. See
writer.csv.sdmxdoes not currently support reading SDMX-CSV 1.0.SDMX-CSV 2.0.0 corresponds to SDMX 3.0.0. The format differs from and is not backwards compatible with SDMX-CSV 1.0. SDMX-CSV 2.0.0 files are recognizable by the header
STRUCTUREin the first column of the first row.reader.csvsupports reading SDMX-CSV 2.0.0.Added in version 2.19.0: Initial support for reading SDMX-CSV 2.0.0.
writer.csvsupports writing SDMX-CSV 2.0.0. Currently, onlyKeys.noneis supported; passing any other value raisesValueError.Added in version 2.23.0: Initial support for writing SDMX-CSV 2.0.0.
Information about SDMX-CSV file formats.
- class sdmx.format.csv.common.Attributes(*values)[source]¶
Attributes to include.
- group_key = 4[source]¶
Attributes attached to any (0 or more)
GroupKeyassociated with each Observation.
- observation = 1[source]¶
Attributes attached to each
Observation.
- class sdmx.format.csv.common.CSVFormatOptions(labels: Labels = Labels.id, time_format: TimeFormat = TimeFormat.original)[source]¶
SDMX-CSV format options.
These options and default values are common to SDMX-CSV 1.0, 2.0.0, and 2.1.0.
- time_format: TimeFormat = 1[source]¶
Time format.
- class sdmx.format.csv.common.Labels(*values)[source]¶
SDMX-CSV ‘labels’ parameter.
- id = 1[source]¶
Display only
IdentifiableArtefact.id, forDimensionorDataAttributein column headers andCodein data rows.
- class sdmx.format.csv.common.TimeFormat(*values)[source]¶
SDMX-CSV ‘timeFormat’ parameter.
- sdmx.format.csv.common.kwargs_to_format_options(kwargs: dict, cls: type[CSVFormatOptions]) None[source]¶
Separate from kwargs any attributes of
CSVFormatOptions.
SDMX-CSV 1.0 format.
- class sdmx.format.csv.v1.FormatOptions(labels: Labels = Labels.id, time_format: TimeFormat = TimeFormat.original)[source]¶
Format options for SDMX-CSV version 1.0.
SDMX-CSV 2.x formats.
- class sdmx.format.csv.v2.FormatOptions(labels: Labels = Labels.id, time_format: TimeFormat = TimeFormat.original, keys: Keys = Keys.none, custom_columns: list[bytes] = <factory>, delimiter: str = ', ', delimiter_sub: str = '')[source]¶
SDMX-CSV 2.x format options.
- custom_columns: list[bytes][source]¶
“Custom columns” detected by
Reader.inspect_header().
SDMX-JSON¶
Reference: https://github.com/sdmx-twg/sdmx-json
Based on JavaScript Object Notation (JSON). The SDMX-JSON format is versioned differently from the overall SDMX standard:
SDMX-JSON 1.0 corresponds to SDMX 2.1. It supports only data and not structures or metadata.
SDMX-JSON 2.0.0 corresponds to SDMX 3.0.0. It adds support for structures.
See
reader.json.
Added in version 0.5: Support for reading SDMX-JSON 1.0.
SDMX-ML¶
Reference: https://github.com/sdmx-twg/sdmx-ml
Based on eXtensible Markup Language (XML). SDMX-ML can represent every class and property in the IM.
An SDMX-ML document contains exactly one
Message. Seesdmx.messagefor the different classes of Messages and their attributes.
Added in version 2.11.0: Support for reading SDMX-ML 3.0.0.
- class sdmx.format.xml.common.XMLFormat(model, base_ns: str, class_tag: Iterable[tuple[str, str]])[source]¶
Information about an SDMX-ML format.
- sdmx.format.xml.common.construct_schema(schema_dir: Path | None = None, version: str | Version = Version.2.1) XMLSchema[source]¶
Construct a
lxml.etree.XMLSchemafor SDMX-ML of the given version.SDMXCommon.xsdincludes the documentation:XHTMLType allows for mixed content of text and XHTML tags. When using this type, one will have to provide a reference to the XHTML schema, since the processing of the tags within this type is strict, meaning that they are validated against the XHTML schema provided.
This function does so by inserting an
<xs:import>element that refers to http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd, which is the URL given by https://www.w3.org/TR/xhtml1-schema. With theXMLSchemareturned by this document, it is possible to validate<common:StructuredText>elements that representXHTMLAttributeValue.
Format API¶
Common code for describing SDMX data formats.