JOGUE AGORA

Loading RDF from the Web using rdflib


� � 
LIVE � �  � � 


04.05.2024

Hey guys how's it going I'm going to be. talking about the RDF library more. specifically about the loading and. saving the RDF graphs now in the. previous video I gave a general. introduction to the RDF library and if. you haven't seen it I highly suggest you. to check it out okay so let's get. started now first things first we want. to make sure that their libraries in. Stowe can do it like so with it now I. have already installed the library. that's why it's so quick now let's. import the library and we can start so. first of all we want to initialize the. graph instance and then let's imagine. that we have a graph which is stored on. the web now what we can do is simply use. the parse function and give it a URL URL. address to the graph so let's check out. the graph now this is how the media. graph looks like about the Python.

Programming language and T so what we. can do as I said before we can just pass. it the URL and the method will handle. everything by itself so let's check out. the number of statements we can see that. the graph has 801 statements now when it. comes to handling the graph like. manipulating the graph at the first. video now let's check out my directory. now we can see that in my directory I. have created a example that XML file so. let's take a look at the graph now we. can see that it is a very simple graph. it contains just two entries some some. namespaces and two entities so basically. a new statement or fact so what we can. do is in order to load the graph or. parse it we have to first initialize the. graph instance again and then instead of. passing a URL or. we pass it after the file so in this. case it is just example that XML because.

The file is located in my current. working directory and in this case we. have to specify a format or basically a. parser that it's going to be used a. parser engine now if we are not sure. about what format you use we can use the. RDF utility gasma. yes format method which takes as an. argument again only the path to the file. and this is going to determine what. format to use okay so let's run this we. can see that the graph has been for has. been unloaded and let's print the number. of statement and let's you realize the. graph so we can see that the graph H has. two fact and when we serialized the. graph back and decoded it we can see. that it is identical to do original file. now we can of course whatever you wanted. with the graph now now that it is loaded. for example we can iterate over it. strapless and print them out okay so in.

This third example let's imagine that we. have a string which is loaded memory. like so and we want to use the parser to. parse the graph for us so what we can do. is again use the parse method but this. time we are going to specify the data. argument and provided the string or the. data and again we have to provide the. format and if we run this we can see. that the graph has been loaded and when. we print out the size of the graph we. can see that it contains two facts. now let's iterate over its subject. predicate and object and we can see that. the. the data has been nicely parsed and we. can actually work work with individual. items in the graph as we please now. let's talk about saving saving the graph. and so first thing first we can do is. use the serialized again and use the bit. code method to simply print it to the.

Standard output specifying the format so. basically with the first graph add a. graph and which was original and in. three triple format and then we. serialized it into the turtle format and. that's why it looks like this now. similarly if we want to save it to a. file instead of passing nothing we. basically give it a tap and since I want. to save it into current directory I can. just give it the name of the file. specify the format and let's run this. and let's look at my directory here we. can see that the that the file is here. and just double check that I didn't. treat so I'm going to give it a tooth. here now we can see that the file is. here and if I Pat B file we can see that. the triples are basically the same as in. here yes so and this is the RTF library. more specifically the loading and saving. the data files of the RDF graphs and.

that's pretty much it so thank you for. watching

All Devices iOS Android Chromecast