使用变量在python中创建N三元组

时间:2013-06-17 11:53:03

标签: python variables rdf turtle-rdf

我目前正在尝试使用变量来创建N三元组的图形,我在分配变量时没有任何问题,但我不断收到错误消息。这是代码:

from rdflib import Namespace, URIRef, Graph
from StringIO import StringIO


xmlns = "http://www.example.org/lexicon#"
rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
lemon = "http://www.monnetproject.eu/lemon#"
graph = Graph()
F = open("new_2.txt", "r")
for line in F:

这是我分配变量的部分。这很长,我知道它有效,所以我不会包括这个。以下代码仍然是for line in F:

的一部分
line1 = ''+synset_offset+'\rdf.lex_filenum\ '+lex_filenum+''
    line2 = ''+synset_offset+'\lemon.ss_type\ '+ss_type+''
    line3 = ''
    for item in word: 
        line3 +=''+synset_offset+'\lemon.lexical_entry\ '+iw.next()+'/n'
    line4 = ''+synset_offset+'\lemon.gloss\ '+gloss+''
    line5 = ''
    line6 = ''
    line7 = ''
    for item in S:
        pointer = ip.next()
        pos = iss.next()
        source_target = ist.next()
        line5 += ''+synset_offset+'\lemon.has_ptr\ '+pointer+'/n'
        line6 += ''+pointer+'\lemon.pos\ '+pos+'/n'
        line7 += ''+pointer+'\lemon.source_target\ '+source_target+'/n'

    contents = '''\
    '''+line1+'''
    '''+line2+'''
    '''+line3+'''
    '''+line4+'''
    '''+line5+'''
    '''+line6+'''
    '''+line7+''''''  
    tabfile = StringIO(contents)
    for line in tabfile:
        triple = line.split()                # triple is now a list of 3 strings
        triple = (URIRef(t) for t in triple) # we have to wrap them in URIRef
        graph.add(triple)

print graph.serialize(format='nt')  

这是我正确打印所有内容的其他代码,表明它不是不起作用的变量。

print('''<http://example.org/#'''+synset_offset+'''> <http://www.monnetproject.eu/lemon#lex_filenum> "'''+lex_filenum+'''".
<http://example.org/#'''+synset_offset+'''> <http://www.monnetproject.eu/lemon#ss_type> "'''+ss_type+'''".
<http://example.org/#'''+synset_offset+'''> <http://www.monnetproject.eu/lemon#gloss> "'''+gloss+'''".''')
    for item in word:
        print('''<http://example.org/#'''+synset_offset+'''> <http://www.monnetproject.eu/lemon#lex_entry> "'''+iw.next()+'''".''')

    for item in S:
        pointer = ip.next()
        pos = iss.next()
        source_target = ist.next()
        print('''<http://example.org/#'''+synset_offset+'''> <http://www.monnetproject.eu/lemon#has_ptr> "'''+pointer+'''".
<http://example.org/#'''+pointer+'''> <http://www.monnetproject.eu/lemon#pos> "'''+pos+'''".
<http://example.org/#'''+pointer+'''> <http://www.monnetproject.eu/lemon#source_target> "'''+source_target+'''".''')
    print('''\n''')

任何比我在这里所做的更好的想法都会受到欢迎

编辑:现在我收到了这个错误:

graph.add(triple)
  File "/usr/lib/python2.7/site-packages/rdflib-4.1_dev-py2.7.egg/rdflib/graph.py", line 352, in add
    def add(self, (s, p, o)):
ValueError: need more than 2 values to unpack

1 个答案:

答案 0 :(得分:2)

您尝试将“方法”与str连接起来,这意味着iw.next是方法的“指针”,iw.next()将是它的返回值,这就是你想。

明确:

line3 +=''+synset_offset+'\lemon.lexical_entry\ '+iw.next()+'/n'

更新(关于下一个错误):

triple必须是包含3个元素的tuple,就像函数签名所说:

add(self, (s, p, o))

忽略self,因为您正在调用实例方法。

我很确定triple有另一种类型,所以请检查一下(最简单的方法是print triple语句中的for

相关问题