用Python读一个Turtle / N3 RDF文件 [英] Reading a Turtle/N3 RDF File with Python

查看:2580
本文介绍了用Python读一个Turtle / N3 RDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Turtle 格式的一些植物数据编码,使用 RDFLib 从Python读取此数据。但是,我有麻烦,我不知道是不是因为我的龟是畸形的,或者我是滥用 RDFLib。

I'm trying to encode some botanical data in Turtle format, and read this data from Python using RDFLib. However, I'm having trouble, and I'm not sure if it's because my Turtle is malformed or I'm misusing RDFLib.

我的测试数据是:

@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', []]]

我尝试改变parse()参数,但是一切都给我一个错误。我几乎没有发现关于如何解析龟的例子。我做错了什么?

I've tried varying the parse() parameters, but everything gives me an error. I've found little to no examples on how to parse Turtle. What am I doing wrong?

推荐答案

我认为第一个问题是大写 PREFIX - 如果你把它超过了那个点。不确定是否是rdflib或Turtle .ttl 中的错误,但 Turtle Validator 在线演示似乎同意这是 .ttl 的问题(说验证失败:@不支持PREFIX指令,第1行col 0。,但如果您小写,则会出现问题。)

I think the first problem is w/the uppercase PREFIX-- if you lowercase those it gets past that point. Not sure if it's a bug in rdflib or in the Turtle .ttl, but the Turtle Validator online demo seems to agree it's a problem with the .ttl (says Validation failed: The @PREFIX directive is not supported, line 1 col 0. but that problem goes away if you lowercase them).

一旦你过去了障碍,两个解析器都不喜欢 p:given_by [:预期的错误语法('])在^ in:... per rdflib;海龟验证者说

Once you're past that hurdle, neither parser likes the part around p:given_by [: "Bad syntax (']' expected) at ^ in:"... per rdflib; Turtle Validator says

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

所以它特别不喜欢 p:source / Philip_Miller 部分。

so it specifically dislikes the p:source/Philip_Miller part.

从这两个问题(谁知道是否还有其他人...)我想你可以得出结论,这N3来源(您发布的 .ttl 文件)已损坏,并将您的注意力转移到第一个使用这个文件制作的系统,为什么使它成为一种多重破坏的方式。

From these two issues (who knows if there are others...!) I think you can conclude that this N3 source (the .ttl file you post) is broken, and turn your attention to whatever system made this file in the first place, and why it's making it in such a multiply broken way.

这篇关于用Python读一个Turtle / N3 RDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆