Save owl rdf/xml to string without writing to disk

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Save owl rdf/xml to string without writing to disk

jritch33
Hi Jiba,

Loving Owlready2, great stuff!

I have a web container where I cannot write to disk.

I need to create an ontology using Owlready2 and store the xml/rdf text in a string format. The only way I can do it now is add a path to onto_path and call the save method (which writes to disk) and then read it back into a string variable.

So I need to bypass the save method and store the xml/rdf straight from memory to string. How can I accomplish this with Owlready2?

Thanks,
Jordon
Reply | Threaded
Open this post in threaded view
|

Re: Save owl rdf/xml to string without writing to disk

Jiba
Administrator
Hi,

When calling save(), you can pass a BytesIO object instead of a file or a file name, e.g.

from io import *

f = BytesIO()
onto.save(f)
print(f.getvalue())

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Save owl rdf/xml to string without writing to disk

jritch33
Thank you for your response, I was able to get the value of the owl file without saving to disk.

Can I also load the ontology without using disk?

I tried: get_ontology(iri).load(fileobj = f) but received an error.

Thanks,
Jordon
Reply | Threaded
Open this post in threaded view
|

Re: Save owl rdf/xml to string without writing to disk

Jiba
Administrator
Hi,

get_ontology(iri).load(fileobj = f) should work.

Could you show me the error message and the code, please ?

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Save owl rdf/xml to string without writing to disk

jritch33
Hi,

Appears I had a typo in my iri. After I corrected that I was able to load the ontology from a byte object as well as the decoded string.

Sorry for the trouble, really appreciate your diligence in supporting this forum. Super helpful.

Jordon