我有这个代码:
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
Run Code Online (Sandbox Code Playgroud)
其中输出是单个元素元组的列表(例如,我输入we all there):(
[('tore',), ('ngaming',), ('sittam',)]将输入翻译成母语).我想将输出转换为字符串以消除[],(),'','符号.如何将其转换为字符串?
你必须使用str.join方法.
>>> a = [('tore',), ('ngaming',), ('sittam',)]
>>> " ".join([x[0] for x in a])
'tore ngaming sittam'
Run Code Online (Sandbox Code Playgroud)