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)
添加回答
举报