2013-03-05 74 views
0

我試圖將SQL中的行寫入文本文件。在文件中寫入列

我的代碼:

result = 'C:/Users/Desktop/SWN_all_result.txt' 
result_file = open(result, 'w') 
sql_select2 = "select * from test01" 
c.execute(sql_select2) 
rows2 = c.fetchall() 
for detail2 in rows2: 
    Sentence = detail2[1] 
    SWN_Note = detail2[4] 
    print Sentence, '\t', SWN_Note, '\n' 
    result_file.write(Sentence + '\t' + SWN_Note + '\n') 
result_file.close() 
db.close() 

結果表明:

abc 
    neutral 

cdf 
    positive 

我怎樣才能讓他們在同一行,因爲我在代碼中提到?

我錯過了什麼嗎?

回答

1

您似乎必須在數據中有換行符。在打印和寫入文件之前剝去它們:

Sentence = detail2[1].rstrip('\n')