16

I'm trying to generate RDF data using RDFLib in Python 3.4.

A minimal example:

from rdflib import Namespace, URIRef, Graph
from rdflib.namespace import RDF, FOAF

data = Namespace("http://www.example.org#")

g = Graph()

g.add( (URIRef(data.Alice), RDF.type , FOAF.person) )
g.add( (URIRef(data.Bob), RDF.type , FOAF.person) )
g.add( (URIRef(data.Alice), FOAF.knows, URIRef(data.Bob)) )

#write attempt
file = open("output.txt", mode="w")
file.write(g.serialize(format='turtle'))

This code results in the following error:

file.write(g.serialize(format='turtle'))
TypeError : must be str, not bytes

If I replace the last line with:

file.write(str(g.serialize(format='turtle')))

I do not get the error, but the result is a string representation of a binary stream (a single line of text starting with b'):

b'@prefix ns1: <http://xmlns.com/foaf/0.1/> .\n@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n@prefix xml: <http://www.w3.org/XML/1998/namespace> .\n@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n\n<http://www.example.org#Alice> a ns1:person ;\n    ns1:knows <http://www.example.org#Bob> .\n\n<http://www.example.org#Bob> a ns1:person .\n\n'

Question How do I correctly export the graph into a file?

jwpfox
  • 5,124
  • 11
  • 45
  • 42
Arthur Vaïsse
  • 1,551
  • 1
  • 13
  • 26

4 Answers4

21

The serialize method accepts a destination keyword that is a file path. In your example, you would want to use:

g.serialize(destination='output.txt', format='turtle')

Instead of

file = open("output.txt", "w")
file.write(g.serialize(format='turtle'))
white_gecko
  • 4,808
  • 4
  • 55
  • 76
Ted Lawless
  • 830
  • 6
  • 6
  • @lawlesst: I am having the same problem however, if I don't want to change the directory the serializer works fine but as soon as I change it to another drive it returns IOError: [Errno 2] No such file or directory: Skane/Output/Skane/ontolog_output/NVDB_RA1/NVDB_RNO_V5042_RDF.owl. as you see the directory name is missing which is due to useing urlparse() function. Am I missing sth or is it a bug in the code? – msc87 Jul 23 '15 at 13:07
  • @msc87 I haven't encountered this problem. RDFLib is able to serialize files to other paths. I use this regularly. Are you providing the absolute file path? I would try that. Also try something like "/tmp/myfile.owl" and seef if you get a similar errorr. – Ted Lawless Jul 26 '15 at 18:27
  • @lawlesst: D:/Skane/Output/Skane/ontolog_output/NVDB_RA1/NVDB_RNO_V5042_RDF.owl is the path I defined. it does not return any error with /tmp and the file is created. using Skane/Output/Skane/ontolog_output/NVDB_RA1/NVDB_RNO_V5042_RDF.owl also works and it creates the file in the c directory but if i change it to D it returns the error and if I used "d" it does not return the error neither creates the file. – msc87 Jul 27 '15 at 11:22
  • @msc87 this seems like a Windows file path issue. The backslashes need to be escaped. See [this primer](http://www.diveintopython.net/file_handling/os_module.html) from Dive into Python. – Ted Lawless Jul 27 '15 at 13:23
2

I have had exactly the same problem working in Python 3.7.3. Using the 'destination' parameter, as suggested in previous answer, did not help me, since I wanted the triples to be appended to the RDF file. I understand that the problem comes from the fact that in Python3, byte is the data structure that replaced Python2 strings. Setting the 'encoding' parameter of the serialize method also did not work. I found a working solution in this post: to decode the resulting string. Instead

g.serialize(format='turtle')

use

g.serialize(format='turtle').decode('utf-8')

or whatever format you are using. Hope that helps.

julthep
  • 15
  • 1
  • 7
Polina
  • 21
  • 2
  • thank you so much, this was exactly the problem we hit when we refactored code to handle unicode – Paco Jan 02 '20 at 19:20
2

Writing the file name in the function worked for me:

g.serialize('output_file.ttl',format='ttl')
Enayat
  • 3,904
  • 1
  • 33
  • 47
-1

g.serialize(destination="file:G:/RootFolder/Tests_rdflib/Test_rdflib_1/output2.xml", format='xml')

  • Welcome to Stack Overflow. When you post an answer, please explain the code you are posting, and provide some details. See : https://stackoverflow.com/help/how-to-answer – Fukiyel Mar 09 '19 at 11:14
  • Please some explanation with your code in [answer](https://stackoverflow.com/help/how-to-answer). – Partho63 Mar 09 '19 at 11:15