Skip to main content

RDF 1.2 converter between N-Triples and Turtle (no rdflib dependency)

Project description

rdf_converter.py (RDF 1.2 N-Triples <-> Turtle)

This README documents only rdf_converter.py.

rdf_converter.py is a self-contained Python parser/serializer/converter for:

  • RDF 1.2 N-Triples (.nt)
  • RDF 1.2 Turtle (.ttl, .turtle)

It does not depend on rdflib for parsing or conversion.

Features

  • Parse RDF 1.2 N-Triples into an internal typed triple model
  • Parse RDF 1.2 Turtle (with base IRI support for relative IRIs)
  • Convert N-Triples -> Turtle
  • Convert Turtle -> N-Triples
  • Deterministic serialization
  • Optional readable Turtle output (grouping, object lists, prefix generation)
  • Optional validation-only mode (parse without writing output)
  • Optional graph/statistics reporting

Requirements

  • Python 3.10+ (uses modern type syntax, e.g. X | Y)

Installation

From PyPI (after publishing):

pip install rdf12conv

CLI command after installation:

rdf12conv INPUT [OUTPUT]

CLI Usage

python3 rdf_converter.py INPUT [OUTPUT]

INPUT / OUTPUT can be file paths or - (stdin/stdout).

Examples:

# Convert by file extension
python3 rdf_converter.py input.nt output.ttl
python3 rdf_converter.py input.ttl output.nt

# Convert using stdin/stdout (explicit formats required)
python3 rdf_converter.py --from nt --to turtle - -

# Validate only (no output path required)
python3 rdf_converter.py --validate-only input.ttl

Format Selection

Formats can be inferred from file extensions or passed explicitly.

Supported aliases:

  • nt, ntriples, n-triples
  • ttl, turtle

Use:

  • --from ... for input format
  • --to ... for output format

If inference is not possible (for example - / stdin), provide the format explicitly.

Options

Core options:

  • --from {nt,ntriples,n-triples,ttl,turtle}
  • --to {nt,ntriples,n-triples,ttl,turtle}
  • --base BASE_IRI (base IRI for Turtle input; defaults to input file URI when possible)
  • --validate-only (parse only, do not write output)
  • --stats (print graph/serialization stats to stderr)

Turtle output options (valid only when target format is Turtle):

  • --turtle-style {readable,minimal}
  • --lists {auto,off}
  • --auto-prefixes {on,off}
  • --prefix PREFIX=IRI (repeatable)
  • --output-base IRI (emit @base)

Validation rules enforced by the CLI:

  • OUTPUT is optional only with --validate-only
  • source and target formats must differ when converting
  • Turtle-only options are rejected for non-Turtle output
  • conflicting repeated --prefix bindings are rejected

Turtle Output Modes

--turtle-style readable (default):

  • groups triples by subject
  • groups repeated predicates/objects with ; and ,
  • can compact RDF collections to Turtle lists ((...))
  • can emit @prefix directives automatically

--turtle-style minimal:

  • writes one triple per line
  • avoids readability-oriented compaction

Examples

# Validate N-Triples and print stats
python3 rdf_converter.py --validate-only --stats input.nt

# Turtle output without list compaction
python3 rdf_converter.py --lists off input.nt output.ttl

# Minimal Turtle output
python3 rdf_converter.py --turtle-style minimal input.nt output.ttl

# Manual prefixes only
python3 rdf_converter.py --auto-prefixes off \
  --prefix ex=http://example.org/ \
  --prefix foaf=http://xmlns.com/foaf/0.1/ \
  input.nt output.ttl

# Emit @base in generated Turtle
python3 rdf_converter.py --output-base http://example.org/base/ input.nt output.ttl

Python API (Library Use)

rdf_converter.py can also be imported as a module.

Main functions:

  • parse_ntriples(text, source="<string>") -> list[Triple]
  • parse_turtle(text, source="<string>", base_iri=None) -> list[Triple]
  • serialize_ntriples(triples) -> str
  • serialize_turtle(triples, options=None) -> str
  • serialize_turtle_with_meta(triples, options=None) -> tuple[str, dict[str, int]]
  • convert_data(...) -> tuple[str, list[Triple], dict[str, int]]

Common data types:

  • IRI
  • BNode
  • Literal
  • TripleTerm
  • ParseError
  • TurtleSerializationOptions

Example:

from rdf_converter import parse_turtle, serialize_ntriples

ttl = '<http://example/s> <http://example/p> "x" .\\n'
triples = parse_turtle(ttl, source="example.ttl", base_iri="http://example/")
nt = serialize_ntriples(triples)
print(nt)

Error Handling

  • Syntax and deterministic parse failures raise ParseError
  • CLI validation/configuration errors are reported as Error: ... and exit with status 1

Notes

  • The implementation follows the repository grammars in grammar/ntriples.bnf and grammar/turtle.bnf
  • Turtle parsing supports base IRI resolution for relative IRIs
  • Serialization output is designed to remain parseable by the project parser

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rdf12conv-0.1.0.tar.gz (20.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rdf12conv-0.1.0-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file rdf12conv-0.1.0.tar.gz.

File metadata

  • Download URL: rdf12conv-0.1.0.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for rdf12conv-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8bb1852e32ea1f669eb59490ecf1f274d871cec626d03b70dadb4c8f1031526a
MD5 b78da7d95696593188c3f8cd23d98076
BLAKE2b-256 3580da7ca95f4fe1abfa85157956e2af6cdeaf47bc956f4b4019357d63e82b4b

See more details on using hashes here.

File details

Details for the file rdf12conv-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: rdf12conv-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for rdf12conv-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7b97c6f0a03bb96dd3981994771f664fecea09be4dd126fcfbe4b9a27a68116f
MD5 696319f431fd53737f874852b59782b7
BLAKE2b-256 e22501c2d132a1e87256dfae2ca3966dc4b4f74c53fd8a7ebed6846d83713356

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page