2017-03-03 220 views
1

在python中,我從數據庫獲取了fetchall數據。在Python中將fetch_all數組轉換爲單個數組

SENTENCE = "It is wonderful. I'am happy" 
sent= (word_tokenize(SENTENCE)) 

cursor = conn.cursor() 
format_strings = ','.join(['%s'] * len(sent)) 
cursor.execute("SELECT emotion_type FROM emotion WHERE key_word IN (%s)" % format_strings,tuple(sent)) 
results = cursor.fetchall() 
for i in results: 
     z= (i) 

輸出是

('happy',) 
('happy',) 

但我希望得到這個結果作爲

['happy','happy'] 

如果有任何可能的方式來獲得輸出,因爲我想。請幫幫我 !

回答

1

如果結果只會是從一列,那麼你可以隨時做

results = [res[0] for res in cursor.fetchall()] 
+0

謝謝。這正是我真正想要的。 – Chathurika