Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,43 @@ Requires Python 3.6 or later.
pip install csvwlib
```

## Usage from console

## Usage
`csvwlib` can be used directly from the console via the `csvwlib` command-line tool (installed automatically with the package).

### Basic Usage

```bash
csvwlib [options]
```

Convert a CSV file (with optional metadata) into RDF (Turtle):

```bash
csvwlib -c https://w3c.github.io/csvw/tests/test001.csv -f ttl -o output.ttl
```

Convert using **metadata only** (metadata references the csv):

```bash
csvwlib -m https://w3c.github.io/csvw/tests/test001.json -o table.json
```


### Options

| Option | Description |
| ----------------- | -------------------------------------------------------------------------- |
| `-c, --csv` | CSV file path or URL |
| `-m, --metadata` | Path or URL to CSVW metadata file |
| `-f, --format` | RDF serialization format (e.g. `ttl`, `xml`, `json-ld`); default: `json-ld`|
| `-o, --output` | Output file path |
| `--mode` | Conversion mode (`standard` or `minimal`); default: `minimal` |
| `-h, --help` | Show usage help |



## Usage as python library

The library exposes one class - `CSVWConverter` which has methods `to_json()` and `to_rdf()`

Expand Down Expand Up @@ -53,6 +88,8 @@ From the `rdflib` docs:
If you don't specify the format, you will receive a `rdflib.Graph` object.




## Examples

Example data+metadata files can be found at
Expand Down
41 changes: 41 additions & 0 deletions csvwlib/csvwcli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python3
"""
a command line client for csvwlib
"""

import argparse
from csvwlib import CSVWConverter
import rdflib

def convert(csv_path=None, metadata_path=None, mode="standard", format="json-ld"):
# Run csvwlib conversion
graph = CSVWConverter.to_rdf(csv_path, metadata_path, mode=mode)
# Ensure result is an rdflib.Graph
if not isinstance(graph, rdflib.Graph):
raise RuntimeError("csvwlib did not return an rdflib.Graph")
return graph.serialize(format=format)

def main():
parser = argparse.ArgumentParser(description="Convert CSV + metadata to JSON-LD with csvwlib")
parser.add_argument("--csv", "-c", help="URL to a CSV file (optional if metadata references it)")
parser.add_argument("--meta", "-m", help="URL to a CSVW metadata JSON")
parser.add_argument("--out", "-o", help="Output RDF file (default: print to stdout)")
parser.add_argument("--mode", default="minimal", choices=["standard", "minimal"],
help="CSVW conversion mode")
parser.add_argument("--format", "-f", default="json-ld",
help="A rdf format to serialize (default: json-ld)",
choices=["json-ld","xml","n3","nt","ttl","trig","nquads"])
args = parser.parse_args()

myrdf = convert(args.csv, args.meta, mode=args.mode, format=args.format)

if args.out:
with open(args.out, "w", encoding="utf-8") as f:
f.write(myrdf)
print(f"✔ Wrote rdf to {args.out}")
else:
print(myrdf)


if __name__ == "__main__":
main()
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@
"language-tags >= 0.4.3"
],
zip_safe=False,

entry_points={
"console_scripts": [
"csvwlib=csvwlib.csvwcli:main"
],
},
keywords = ", ".join(KEYWORDS),
classifiers = [
"Programming Language :: Python :: 3",
Expand Down