我目前正在尝试使用变量来创建 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