在python中创建三元组时变量工作不正常

2024-10-05 13:14:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我的想法是在Wordnet文本行中,将行的所有不同部分分配给不同的变量,并将这些变量作为三元组输入到RDFlib图中。你知道吗

以下是文本文件中的一行示例:

13797906 23 n 04 flood 0 inundation 0 deluge 0 torrent 0 005 @ 13796604 n 0000 + 00603894 a 0401 + 00753137 v 0302 + 01527311 v 0203 + 02361703 v 0101 | an overwhelming number or amount; "a flood of requests"; "a torrent of abuse"

这是我的密码。你知道吗

from rdflib import URIRef, Graph
from StringIO import StringIO

G = Graph()
F = open("new_2.txt", "r")
for line in F:

    L = line.split()
    L2 = line.strip().split('|')
    synset_offset = L[0]
    lex_filenum = L[1]
    ss_type = L[2]
    gloss = L2[1]                                   
    before_at, after_at = line.split('@', 1)
    N = int(L[3])
    K = int(before_at.split()[-1])                                     
    word = L[4:4 + 2 * N:2]                         
    iw = iter(word)
    S = after_at.split()[0:0 +4 * K:4]              
    ip = iter(S)
    SS = after_at.split()[1:1 + 4 * K:4]            
    iss = iter(SS)
    ST = after_at.split()[2:2 + 4 * K:4]          
    ist = iter(ST)

    line1 = '''http://www.example.org/lexicon#'''+synset_offset+''' http://www.w3.org/1999/02/22-rdf-syntax-ns#lex_filenum '''+lex_filenum+''''''
    line2 = '''http://www.example.org/lexicon#'''+synset_offset+''' http://www.monnetproject.eu/lemon#ss_type '''+ss_type+''''''             
    line3 = ''''''
    #line4 = '''http://www.example.org/lexicon#'''+synset_offset+''' http://www.monnetproject.eu/lemon#gloss '''gloss'''                         
    for item in word: 
        line3 += '''http://www.example.org/lexicon#'''+synset_offset+''' http://www.monnetproject.eu/lemon#lexical_entry '''+iw.next()+'''\n'''   
    line5 = ''''''
    for item in S:
        line5 += '''http://www.example.org/lexicon#'''+synset_offset+''' http://www.monnetproject.eu/lemon#has_ptr '''+ip.next()+'''\n'''            
    line6 = ''''''
    for item in SS:
        line6 += '''http://www.example.org/lexicon#'''+ip.next()+''' http://www.monnetproject.eu/lemon#pos '''+iss.next()+'''\n'''                      
    line7 = ''''''
    for item in ST:
        line7 += '''http://www.example.org/lexicon#'''+ip.next()+''' http://www.monnetproject.eu/lemon#source_target '''+ist.next()+'''\n'''     

    contents = '''\
    '''+line1+'''
    '''+line2+''' 
    '''+line3+'''
    '''+line5+'''
    '''+line6+'''  
    '''+line7+''''''#'''+line4+'''

    tabfile = StringIO(contents)
    for line in tabfile:
        triple = line.split()
        triple = (URIRef(t) for t in triple)
        G.add(triple)

print G.serialize(format='nt')

line5之前,一切都是完美的。(第4行由于不同的原因被注释掉了,我还不需要它)

这是包含第5行、第6行和第7行时出现的错误:

 G.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 0 values to unpack

我不明白第3行和第5行的区别是什么,这会导致一个错误,第3行工作完美!你知道吗


Tags: inorghttpforexamplewwwlineat

热门问题