Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert single element tuple into string?

I have this code:

 import nltk
 import pypyodbc

 text = raw_input()
 token = nltk.word_tokenize(text) //return a list value


def search(self, lists):
    if not self.connected:
        self.connect()
    for word in lists:
        self.cur.execute('SELECT Ybanag FROM Words WHERE English IN (%s)' % (','.join('?'*len(lists))), lists)
        result = self.cur.fetchall()
        return result

wherein the output is a list of single element tuple (ex. I enter we all there): [('tore',), ('ngaming',), ('sittam',)] (translate the input into mother tongue language). I want that the output will be converted into string to eliminate the [],(),'',' symbols. How to convert it into string?


1 Answers

You have to use str.join method.

>>> a = [('tore',), ('ngaming',), ('sittam',)]
>>> " ".join([x[0] for x in a])
'tore ngaming sittam'
like image 82
Nilesh Avatar answered Dec 14 '25 02:12

Nilesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!