4 回答
TA貢獻1812條經驗 獲得超5個贊
因為其他人已經給了你一些答案,所以你在 Python 3.x 中:
print (*sentence,sep='\n',file=open(os.path.join(path, 'testlist.csv'), 'w'))
或者在 Python 2.7 中你可以這樣做:
print open(os.path.join(path, 'testlist.csv'), 'w'),"\n".join(sentence)
(以上都不需要 csv 模塊)
在你的例子中,我認為你可以改變
f_writer = csv.writer(my_file)
到
f_writer = csv.writer(my_file, delimiter='\n')
在真正的延伸中,您可能可以改為更改:
f_writer.writerow(sentence)
到
f_writer.writerows(list([x] for x in sentence))
快樂的Python!
TA貢獻1846條經驗 獲得超7個贊
write row 取一個列表并將其寫在一行中,,如果您希望該行的元素在單獨的行上,則分隔
一個一個地傳給他們
sentence = ['this stuff', 'is not','that easy']
with open(os.path.join(path, 'testlist.csv'), 'w') as my_file:
f_writer = csv.writer(my_file)
for s in sentence: f_writer.writerow([s])
# f_writer.writerow(sentence)
TA貢獻2041條經驗 獲得超4個贊
1)在“句子”中使用列表
2)用“writerows”替換“writerow”
例如:
# Change 1
sentence = [['this stuff'], ['is not'],['that easy']]
with open(os.path.join(path, 'testlist.csv'), 'w') as my_file:
f_writer = csv.writer(my_file)
# Change 2
f_writer.writerows(sentence)
TA貢獻1906條經驗 獲得超10個贊
這有效:
import pandas as pd
sentence = ['this stuff', 'is not','that easy']
sent = pd.Series(sentence)
sent.to_csv('file.csv', header = False)
添加回答
舉報
