Skip to main content
 首页 » 编程设计

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

2024年11月01日7wuhuacong

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

请您参考如下方法:

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

明确:

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

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

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

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

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

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