0

I'm doing my master thesis and I'm working in serval files which most of them have a csv extension .

I have also a foaf file and I have no idea how to modelize and read this kind of file. What should I do?

0548
  • 11
  • 4
  • Please, make your question more concrete. What kind of model do you want to build? Is the foaf file a file with foaf ontology itself or foaf data, etc, etc. I presume, you need a library supporting RDF to read the file in the system you are using. You can also convert it to some other format, CSV for instance. – Roman Susi Jun 22 '15 at 17:10
  • Is it like: http://www.w3.org/People/Berners-Lee/card.rdf ? In any case, for this question to be useful, explain the modelize part. RDF has a very nice graph model and belongs to Semantic Web formats. You file is RDF encoded in XML and can be read by any library or tool, which supports RDF. – Roman Susi Jun 22 '15 at 17:28
  • in the beginning of my file , I have a RDF tag: and after it's only a foaf tag so I guess that I need library supporting RDF . am I wrong? And if I can use CSv , it will be easier for me , so could you help me to do this task ? – 0548 Jun 22 '15 at 17:31
  • the RDF tag is : – 0548 Jun 22 '15 at 17:33

1 Answers1

1

It is a little bit problematic to save RDF data (which "FOAF file" actually is) into some table-like file without sacrificing semantics, because RDF is richer format and converting to CSV is in a way "lossy".

Below is a Python script for naive conversion of RDF into CSV, where each RDF triple is mapped into CSV row (install rdflib library before trying this):

from rdflib import Graph
import csv

g = Graph()
g.parse('http://www.w3.org/People/Berners-Lee/card.rdf', format='xml')

with open('card.csv', 'wb') as csvfile:
    writer = csv.writer(csvfile)
    for triple in g:
        writer.writerow(triple)

To make more sense from RDF one should use some query language and write query (for example, in SPARQL) exactly for the data, which is desired from the RDF file.

Another question/answer which might help you: RDF file to excel-readable format (ttl is another encoding of RDF: RDF/Turtle )

Community
  • 1
  • 1
Roman Susi
  • 4,135
  • 2
  • 32
  • 47
  • thank you for you help, i'm going to try your code , i downloaded python 3.4.3 , is it the right version ? AND i mean by modelize : cluster my file in several columns and for exemple collum A : contains persons' name , column B contains their job etc etc – 0548 Jun 22 '15 at 18:11
  • Take a look at rdflib docs: http://rdflib.readthedocs.org/en/latest/ And Python 3 should be ok. – Roman Susi Jun 22 '15 at 18:14
  • To break file into columns you will need to master SPARQL query or program a script, which handles RDF triples. As said, RDF can be framed in many ways, depending on your needs. Rdflib supports SPARQL queries and is not that complex, so I guess it is a better way and you will learn something new in the process. (Personally, I'd converted CSV into RDF, as RDF tools are good at merging many data sources and making them queryable) – Roman Susi Jun 22 '15 at 18:21
  • I tried many days , how to integrate rdflib in python . and I didn't find any solution . I dowlanded python 2.7 and rdf lib 4.2 luck you told me and I don't know how to unstall this library ( I read that I should use easy_install or pip) but I don't know where (I clicked in setup and it doesn't even work) so how can do all of this ? – 0548 Jul 02 '15 at 16:55
  • I guess, how to install rdflib for Python is a different question. And the answer depends on the operating system being used. The "pip install rdflib" command in the command line should be fine if you have Python and pip installed on your system. It is not enough to click setup.py file, "python setup.py install" is one possible command. – Roman Susi Jul 02 '15 at 19:53
  • My operting system is windows 8.1.I use python 2.7 and I thing that pip is already exist in (C:\Python27\Lib\site-packages). if everything is already exist where should I install the rdf lib ? – 0548 Jul 03 '15 at 15:04
  • Sorry, I do not know how to do it on your system, but I guess all Python libraries (with some exceptions) can be installed in the same way. – Roman Susi Jul 05 '15 at 16:47