2010-08-24 49 views
8

我試圖在編碼格式Turtle一些植物的數據,並使用RDFLib從Python中讀取此數據文件。但是,我遇到了麻煩,而且我不確定是因爲我的海龜畸形還是我是misusing RDFLib。閱讀龜/ N3 RDF與Python

我的測試數據是:

@PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . 
@PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> . 
@PREFIX p: <http://www.myplantdomain.com/plant/description> . 
p:description a rdfs:Property . 
p:name a rdfs:Property . 
p:language a rdfs:Property . 
p:value a rdfs:Property . 
p:gender a rdfs:Property . 
p:inforescence a rdfs:Property . 
p:color a rdfs:Property . 
p:sense a rdfs:Property . 
p:type a rdfs:Property . 
p:fruit a rdfs:Property . 
p:flower a rdfs:Property . 
p:dataSource a rdfs:Property . 
p:degree a rdfs:Property . 
p:date a rdfs:Property . 
p:person a rdfs:Property . 
p:c2a7b9a3-c54a-41f5-a3b2-155351b3590f 
    p:description [ 
     p:name [ 
      p:kingdom "Plantae" ; 
      p:division "Pinophyta" ; 
      p:class "Pinopsida" ; 
      p:order "Pinales" ; 
      p:family "Pinaceae" ; 
      p:genus "Abies" ; 
      p:species "A. alba" ; 
      p:language "latin" ; 
      p:given_by [ 
       p:person p:source/Philip_Miller ; 
       p:start_date "1923-1-2"^^<http://www.w3.org/2001/XMLSchema#date> 
      ] 
     ] ; 
     p:name [ 
      p:language "english" ; 
      p:value "silver fir" 
     ] ; 
     p:flower [ 
      p:gender "male"@en ; 
      p:inflorescence "catkin"@en ; 
      p:color "brown"@en ; 
      p:color "yellow"@en ; 
      p:sense "straight"@en 
     ] ; 
     p:flower [ 
      p:gender "female"@en ; 
      p:inflorescence "catkin"@en ; 
      p:color "pink"@en ; 
      p:color "yellow"@en ; 
      p:sense "straight"@en 
     ] ; 
     p:fruit [ 
      p:type "cone"@en ; 
      p:color "brown"@en 
     ] 
    ] . 

我的Python是:

import rdflib 
g = rdflib.Graph() 
#result = g.parse('trees.ttl') 
#result = g.parse('trees.ttl', format='ttl') 
result = g.parse('trees.ttl', format='n3') 
print len(g) 
for stmt in g: 
    print stmt 

這使我的錯誤:

ValueError: Found @PREFIX when expecting a http://www.w3.org/2000/10/swap/grammar/n3#document . todoStack=[['http://www.w3.org/2000/10/swap/grammar/n3#document', []]] 

我試着改變解析()參數,但一切都給我一個錯誤。我幾乎沒有發現如何分析海龜的例子。我究竟做錯了什麼?

回答

10

我認爲第一個問題是瓦特/ 大寫PREFIX - 如果你小寫那些它通過那個點。不知道它在rdflib或龜.ttl一個bug,但Turtle Validator在線演示似乎都同意這是與.ttl問題(說Validation failed: The @PREFIX directive is not supported, line 1 col 0.但如果你小寫他們,問題消失)。

一旦你超過了這個障礙,那麼這兩個解析器都不喜歡p:given_by [左右的部分:「在...中有錯誤的語法(']')in:」... per rdflib;海龜驗證器說

Validation failed: Expecting a period, semicolon, comma, close-bracket, or close-brace but found '/', line 31 col 33. 

所以它特別不喜歡p:source/Philip_Miller部分。

從這兩個問題(誰知道是否有其他人......!)我認爲你可以斷定這N3源(.ttl文件您發佈)壞了,將注意力轉移到作出這無論系統首先是文件,以及爲什麼它會以這種多重破碎的方式實現。

+0

謝謝。將該行更改爲似乎解決了這個問題。 – Cerin 2010-08-24 15:17:40

+0

@克里斯,很高興知道,謝謝! – 2010-08-24 15:18:30