title: Using variables to create N triples in python
body: <p>I am currently trying to use variables to create a graph of N triples, I am not having any trouble assigning the variables, but i keep getting an error message. Here is the code:</p>
  
  <pre><code>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:
  </code></pre>
  
  <p>This is the part where I assign the variables. This is pretty long and I know it works so I wont include this. This following code is still part of the <code>for line in F:</code></p>
  
  <pre><code>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')  
  </code></pre>
  
  <p>This is the other code i have that prints everything correctly, showing that it is not the variables that are not working.</p>
  
  <pre><code>print('''&lt;http://example.org/#'''+synset_offset+'''&gt; &lt;http://www.monnetproject.eu/lemon#lex_filenum&gt; "'''+lex_filenum+'''".
  &lt;http://example.org/#'''+synset_offset+'''&gt; &lt;http://www.monnetproject.eu/lemon#ss_type&gt; "'''+ss_type+'''".
  &lt;http://example.org/#'''+synset_offset+'''&gt; &lt;http://www.monnetproject.eu/lemon#gloss&gt; "'''+gloss+'''".''')
      for item in word:
          print('''&lt;http://example.org/#'''+synset_offset+'''&gt; &lt;http://www.monnetproject.eu/lemon#lex_entry&gt; "'''+iw.next()+'''".''')
  
      for item in S:
          pointer = ip.next()
          pos = iss.next()
          source_target = ist.next()
          print('''&lt;http://example.org/#'''+synset_offset+'''&gt; &lt;http://www.monnetproject.eu/lemon#has_ptr&gt; "'''+pointer+'''".
  &lt;http://example.org/#'''+pointer+'''&gt; &lt;http://www.monnetproject.eu/lemon#pos&gt; "'''+pos+'''".
  &lt;http://example.org/#'''+pointer+'''&gt; &lt;http://www.monnetproject.eu/lemon#source_target&gt; "'''+source_target+'''".''')
      print('''\n''')
  </code></pre>
  
  <p>Any better ideas than the way I have it done here would be very welcome</p>
  
  <p>EDIT: Now i am getting this error: </p>
  
  <pre><code>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
  </code></pre>