Skip to main content

Lightweight RDF Stream Parser

Project description

Lightweight RDF Stream Parser for Python

A lightweight RDF parser which streams triples directly from disk or standard input without loading the entire graph into memory.

Support N-Triples and N-Quads format

Usage

Read and write to disk.

from rdf import NTriples
from rdf import Literal

with NTriples(path = "./pizzacats.nt", mode = 'r') as g:
    with NTriples(path = "./out.nt", mode = 'w') as h:
        for subject, predicate, object in g.parse():
            if type(object) is Literal and object.language == "en":
                # do stuff
            h.write((subject, predicate, object))

Read / write from standard input / output.

from os import stdin
from rdf import NQuads
from rdf import IRIRef

g = NQuads(data=stdin.read(), mode = 'r')
h = NQuads(mode = 'w')

target = IRIRef("https://example.org/Pizzacat")
for triple in g.parse():
    if triple[0] == target:  # subject
        # do stuff
        h.write(triple)
        
g.close()
h.close()

Adding new triples.

from rdf import IRIRef, Literal, Statement
from rdf import RDF, XSD

EX = IRIRef("https://example.org/")  # define prefix
g = set()

subject = EX + "Pizzacat"
g.add(Statement(subject, RDF+"type", EX+"Cat"))

literal = Literal("Samurai Pizza Cats!!!", datatype=XSD+"string")
g.add(Statement(subject, EX+"tag_phrase", literal))

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

pyRDF-2.1.1.tar.gz (17.9 kB view details)

Uploaded Source

File details

Details for the file pyRDF-2.1.1.tar.gz.

File metadata

  • Download URL: pyRDF-2.1.1.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.3 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.10

File hashes

Hashes for pyRDF-2.1.1.tar.gz
Algorithm Hash digest
SHA256 1cfee772292fbb2f37c2bf9727a1d9c621b6ce7e52f548dcaceaf59674194365
MD5 1161e99ecc9e7ca892e8adf88f649209
BLAKE2b-256 11280d95106c163e26637b1431f43ff6a7a5415be837e846ee1cc1ac011e448e

See more details on using hashes here.

Supported by

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