About
csvwlib is a Python implementation of the W3C
CSV on the Web recommendations.
This enables converting tabular data, and optionally its associated metadata, to a semantic graph in RDF or JSON-LD format.
Tabular data includes CSV files, TSV files, and upstream may be coming from spreadsheets, RDBMS export, etc.
Requires Python 3.6 or later.
Installation
pip install csvwlib
Usage
The library exposes one class - CSVWConverter which has methods to_json() and to_rdf()
Both of these methods have similar API, and require 3+ parameters:
csv_url- URL of a CSV file; defaultNonemetadata_url- optional URL of a metadata file; defaultNonemode- conversion mode; defaultstandard, orminimal
The are three ways of starting the conversion process:
-
pass only
csv_url- corresponding metadata will be looked up based oncsv_urlas described in Locating Metadata -
pass both
csv_urlandmetadata_url- metadata by user will be used. Ifurlfield is set in metadata, the CSV file will be retrieved from that location which can cause, that passedcsv_urlwill be ignored -
pass only
metadata_url- associated CSV files will be retrieved based on metadataurlfield
You can also specify the conversion mode - standard or minimal, the default is standard.
From the W3C documentation:
Standard mode conversion frames the information gleaned from the cells of the tabular data with details of the rows, tables, and a group of tables within which that information is provided.
Minimal mode conversion includes only the information gleaned from the cells of the tabular data.
After conversion to JSON, you receive a dict object, when converting to RDF it is more complex.
If you pass format parameter, graph will be serialized to this format and returned as string.
From the rdflib docs:
Format support can be extended with plugins, but "xml", "n3", "turtle", "nt", "pretty-xml", "trix", "trig" and "nquads" are built in.
If you don't specify the format, you will receive a rdflib.Graph object.
Examples
Example data+metadata files can be found at http://w3c.github.io/csvw/tests/
Starting with CSV:
from csvwlib import CSVWConverter
CSVWConverter.to_rdf("http://w3c.github.io/csvw/tests/test001.csv", format="ttl")
Minimal mode:
from csvwlib import CSVWConverter
CSVWConverter.to_rdf("http://w3c.github.io/csvw/tests/tree-ops.csv", mode="minimal", format="ttl")
Starting with metadata only:
from csvwlib import CSVWConverter
CSVWConverter.to_rdf(metadata_url="http://w3c.github.io/csvw/tests/test188-metadata.json", format="ttl")
Both CSV and metadata URL specified:
from csvwlib import CSVWConverter
CSVWConverter.to_rdf("http://w3c.github.io/csvw/tests/tree-ops.csv", "http://w3c.github.io/csvw/tests/tree-ops.csv", format="ttl")
Starting with metadata:
from csvwlib import CSVWConverter
CSVWConverter.to_json("http://w3c.github.io/csvw/tests/countries.json")
Starting with CSV:
from csvwlib import CSVWConverter
CSVWConverter.to_json("http://w3c.github.io/csvw/tests/test001.csv")
Contributors
Authored by @Aleksander-Drozd
Maintained by @DerwenAI
Metadata
Release files for csvwlib 0.3.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| csvwlib-0.3.2.tar.gz | 24.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| csvwlib-0.3.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 57.7 kB
Release files / csvwlib-0.3.2.tar.gz
| Download URL | csvwlib-0.3.2.tar.gz |
|---|---|
| Size | 24.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d3299db86017b79f164d4e3e9d7df1447b70a3be5690164f19ebdc48e620cfdb
|
|
BLAKE2b-256 checksum How to use checksums |
3f081181e6a57dd4ef439ee449a171f6902f5404ca12be22c66035fa74dc0238
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.4
|
Release files / csvwlib-0.3.2-py3-none-any.whl
| Download URL | csvwlib-0.3.2-py3-none-any.whl |
|---|---|
| Size | 32.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a666e9b6a42579893042129783f3aed7cc4b579dea66957f4ccdddb07e245791
|
|
BLAKE2b-256 checksum How to use checksums |
6265869a58bc0fbdb749c7ca46101261f2856770e33728727a2f8dd2b462e21c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.4
|