Reputation: 21
I have a data as shown in the figure and want to convert it into RDF file. I have use following code
#import libraries
import pandas as pd #for handling csv and csv contents
from rdflib import Graph, Literal, RDF, URIRef, Namespace #basic RDF handling
from rdflib.namespace import FOAF , XSD #most common namespaces
import urllib.parse #for parsing strings to URI's
#Read file
df=pd.read_csv("filename.csv",sep="\t",quotechar='"')
#creating graph
g = Graph()
continent = Namespace('http://example.org/continent/')
loc= Namespace('http://mylocations.org/addresses/')
schema = Namespace('http://schema.org/')
for index, row in df.iterrows():
g.add((URIRef(continent+row[0]), RDF.type, URIRef(continent+'Iso_code')))
g.add((URIRef(continent+row[1]), RDF.type, URIRef(continent+'Continent')))
g.add((URIRef(continent+row[2]), RDF.type, URIRef(continent+'Location')))
g.add((URIRef(continent+row[2]), URIRef(continent+'is_in'), URIRef(continent+row[1])))
g.add((URIRef(continent+row[2]), URIRef(continent+'total_cases'), Literal(row[4], datatype=XSD.integer)))
g.add((URIRef(continent+row[2]), URIRef(continent+'date'), Literal(row[3],datatype=XSD.date)))
# save graph
g.serialize('mycsv2rdf.ttl',format='turtle')
Did I create rdf correctly ? the created file have data like.
@prefix ns1: <http://example.org/continent/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
ns1:AFG a ns1:Iso_code .
ns1:Afghanistan a ns1:Location ;
ns1:date "0101-01-01"^^xsd:date,
"0201-01-01"^^xsd:date,
"0301-01-01"^^xsd:date,
"0401-01-01"^^xsd:date,
"0501-01-01"^^xsd:date,
"0601-01-01"^^xsd:date,
"2401-01-01"^^xsd:date,
"2501-01-01"^^xsd:date,
"2601-01-01"^^xsd:date,
"2701-01-01"^^xsd:date,
"2801-01-01"^^xsd:date,
"2901-01-01"^^xsd:date ;
ns1:is_in ns1:Asia ;
ns1:total_cases 1,
2,
4 .
ns1:Asia a ns1:Continent .
Value is not inserted with date wise. help me what i have to do. how to insert date wise value into graph.
Upvotes: 0
Views: 703
Reputation: 1251
No, your data is not correct, in several ways:
datatime.strtotime()
and then supply the date object to RDFlib's Literal
classIso_code
and created instances of it, e.g. ns1:AFG
, but you've not linked anything to it.Actually, none of your RDF makes sense: you need to have a subject, probably ns1:Afghanistan
(but note that your URI for ns1
contains /continent/
and yet Afghanistan is not a continent) and then links thigs to it. You've declared a series of un-related subjects, e.g. ns1:AFG
, ns1:Afghanistan
, ns1:date
.
You probably want something like this:
@prefix ns1: <http://example.org/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix time: <http://www.w3.org/2006/time#> .
ns1:AFG a ns1:Iso_code .
ns1:Asia a ns1:Continent .
ns1:Afghanistan
a ns1:Location ;
ns1:hasIsoCode ns1:AFG ;
ns1:hasDatedCases [
time:hasTime "2020-02-24"^^xsd:date ;
ns1:totalCases 1 ;
ns1:newCases 1 ;
] ,
[
time:hasTime "2020-02-25"^^xsd:date ;
ns1:totalCases 1 ;
ns1:newCases 0 ;
] ,
...
ns1:is_in ns1:Asia ;
.
Upvotes: 0