N-Quads Format (RDF)
Description
N-Quads is an extension of N-Triples that adds a fourth element (graph context) to each triple. It's used to represent RDF datasets with named graphs. Each line represents a single RDF quad (subject-predicate-object-graph).
File Extensions
.nq- N-Quads files.nquads- N-Quads files (alias)
Implementation Details
Reading
The N-Quads implementation:
- Extends N-Triples implementation
- Parses N-Quads format line by line
- Extracts subject, predicate, object, and graph from each line
- Converts each quad to a dictionary
- Supports streaming for large files
Writing
Writing support:
- Writes one quad per line, including an optional
graphfield - Same subject/predicate/object keys as N-Triples
Key Features
- Line-based: One quad per line
- Graph context: Includes graph identifier
- RDF dataset: Represents RDF datasets with named graphs
- Totals support: Can count total quads
- Streaming: Processes files line by line
Usage
from iterable import open_iterable
# Basic reading
with open_iterable('data.nq') as source:
for row in source:
print(row) # Contains: subject, predicate, object, graph
# Writing
with open_iterable('output.nq', mode='w') as dest:
dest.write({
'subject': 'http://example.org/alice',
'predicate': 'http://xmlns.com/foaf/0.1/name',
'object': 'Alice',
'graph': 'http://example.org/graph',
})
Parameters
encoding(str): File encoding (default:utf8)
N-Quads Format Structure
Format: subject predicate object graph .
- subject: URI or blank node
- predicate: URI
- object: URI, literal, or blank node
- graph: Graph identifier (URI or blank node)
- period: Ends each quad
Limitations
- Line-based format: Each quad must fit on a single line
- No abbreviations: More verbose than Turtle
- Flat structure: Each quad is separate (no grouping)
Compression Support
N-Quads files can be compressed with all supported codecs:
- GZip (
.nq.gz) - BZip2 (
.nq.bz2) - LZMA (
.nq.xz) - LZ4 (
.nq.lz4) - ZIP (
.nq.zip) - Brotli (
.nq.br) - ZStandard (
.nq.zst)
Use Cases
- RDF datasets: Working with RDF datasets with named graphs
- Linked data: Processing linked data with graph context
- Knowledge graphs: Building knowledge graphs with multiple graphs
- Data exchange: RDF dataset exchange
Error Handling
- Missing dependency: optional libraries raise
ImportErrorwith an install hint (pip install 'iterabledata[<extra>]'when an extra exists). - Write mode: read-only formats raise
WriteNotSupportedErrororValueErrorwhen opened withmode="w". - Bad or unsupported input: may raise
ValueError,OSError, or library-specific errors. - See Troubleshooting for decoding, detection, and engine issues.