Python implementation of CSV on the Web
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file csvwlib-0.3.2.tar.gz.
File metadata
- Download URL: csvwlib-0.3.2.tar.gz
- Upload date:
- Size: 24.8 kB
- Tags: Source
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3299db86017b79f164d4e3e9d7df1447b70a3be5690164f19ebdc48e620cfdb
|
|
| MD5 |
f2ecbdf8d7db4eb18a4a3e28da043fc8
|
|
| BLAKE2b-256 |
3f081181e6a57dd4ef439ee449a171f6902f5404ca12be22c66035fa74dc0238
|
File details
Details for the file csvwlib-0.3.2-py3-none-any.whl.
File metadata
- Download URL: csvwlib-0.3.2-py3-none-any.whl
- Upload date:
- Size: 32.8 kB
- Tags: Python 3
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a666e9b6a42579893042129783f3aed7cc4b579dea66957f4ccdddb07e245791
|
|
| MD5 |
b1babac629bc1e20f9fb621f0f199892
|
|
| BLAKE2b-256 |
6265869a58bc0fbdb749c7ca46101261f2856770e33728727a2f8dd2b462e21c
|