aannie
aannie

Reputation: 17

Accessing file in turtle (.ttl) file via Python rdflib

I'm trying to parse the data in .ttl format following suggestions I found here. My approach:

    from rdflib import Graph
    
    file = 'XYZ.ttl'
    graph = Graph()
    graph.parse(file, format='turtle')

However, I get the following error:

IndexError: string index out of range

I don't quite get it, as the majority of people did not have any issues with parsing the file.

Upvotes: 0

Views: 1845

Answers (1)

Nicholas Car
Nicholas Car

Reputation: 1251

@aannie your code appears correct so whatever the issue is, it must be to do with your content, so to fully solve this, I would need to see the data you are trying to parse in.

Having said that, content issues, or parsing issues, usually result in BadSyntax errors, so I don't really know where you/re getting this IndexError from, are you sure it's not in an unrelated piece of your Python code?

A couple of hints:

  • you don't need format='turtle' for RDFlib >= 6.0.0 as this is the default format.
  • you can parse a Python string variable using data='...' and this might help you since you can put all the things in one file

Try running this code that I just tested and then, if you get the same answer I do, try putting that Turtle data for d into a file and doing what you did originally, knowing that the data will work:

from rdflib import Graph

d = """
    PREFIX ex: <https://example.com/thing/>
        
    ex:n a ex:Person ;
         ex:name "Nicholas" ;
         ex:location ex:Brisbane ;
         ex:age 39 ;
    .
    """

graph = Graph()
graph.parse(data=d)

print(len(graph))
# this will print "4" since there are 4 triples in the data "d"

Upvotes: 1

Related Questions