Skip to main content

N-Triples Format (RDF)

Description​

N-Triples is a simple, line-based format for representing RDF (Resource Description Framework) data. Each line represents a single RDF triple (subject-predicate-object). It's one of the simplest RDF serialization formats.

File Extensions​

  • .nt - N-Triples files
  • .ntriples - N-Triples files (alias)

Implementation Details​

Reading​

The N-Triples implementation:

  • Parses N-Triples format line by line
  • Extracts subject, predicate, and object from each line
  • Handles URIs and literals
  • Converts each triple to a dictionary
  • Supports streaming for large files

Writing​

Writing support:

  • Writes one triple per line from subject / predicate / object fields
  • Honors subject_type, object_type, datatype, and language tags

Key Features​

  • Line-based: One triple per line
  • Simple format: Easy to parse and generate
  • RDF format: Represents RDF data
  • Totals support: Can count total triples
  • Streaming: Processes files line by line

Usage​

from iterable import open_iterable

# Basic reading
with open_iterable('data.nt') as source:
for row in source:
print(row) # Contains: subject, predicate, object

# Writing
with open_iterable('output.nt', mode='w') as dest:
dest.write({
'subject': 'http://example.org/alice',
'predicate': 'http://xmlns.com/foaf/0.1/name',
'object': 'Alice',
'object_type': 'literal',
})

Parameters​

  • encoding (str): File encoding (default: utf8)

N-Triples Format Structure​

Format: subject predicate object .

  • subject: URI or blank node
  • predicate: URI
  • object: URI, literal, or blank node
  • period: Ends each triple

Limitations​

  1. Line-based format: Each triple must fit on a single line
  2. No abbreviations: More verbose than Turtle
  3. Flat structure: Each triple is separate (no grouping)

Compression Support​

N-Triples files can be compressed with all supported codecs:

  • GZip (.nt.gz)
  • BZip2 (.nt.bz2)
  • LZMA (.nt.xz)
  • LZ4 (.nt.lz4)
  • ZIP (.nt.zip)
  • Brotli (.nt.br)
  • ZStandard (.nt.zst)

Use Cases​

  • RDF data: Working with RDF datasets
  • Linked data: Processing linked data
  • Knowledge graphs: Building knowledge graphs
  • Data exchange: Simple RDF data exchange

Error Handling​

  • Missing dependency: optional libraries raise ImportError with an install hint (pip install 'iterabledata[<extra>]' when an extra exists).
  • Write mode: read-only formats raise WriteNotSupportedError or ValueError when opened with mode="w".
  • Bad or unsupported input: may raise ValueError, OSError, or library-specific errors.
  • See Troubleshooting for decoding, detection, and engine issues.
  • N-Quads - N-Triples with graph context
  • Turtle - More compact RDF format
  • RDF/XML - RDF in XML format