如何在csv的下一行中创建单词

时间:2018-05-16 17:41:12

标签: python python-2.7

我有一个这样的清单:

['tomorrow', 'space', 'Film']

当我把它写到csv时,它变成了:

tomorrow,space,Film

和我将其写入csv时的期望是这样的:

tommorrow
space
Film

这是我的代码

with open("Noun.csv", 'wb') as n:
        noun = csv.writer(n)
        noun.writerow(list_noun)

如何修复我的代码?

3 个答案:

答案 0 :(得分:1)

您可以使用csv.writerows

import csv
s = ['tomorrow', 'space', 'Film']
with open('filename.csv', 'w') as f:
  write = csv.writer(f)
  write.writerows([[i] for i in s])

答案 1 :(得分:0)

import csv
list_noun = ['tomorrow', 'space', 'Film']
with open('noun.csv', 'w') as f:
  [csv.writer(f).writerow([word]) for word in list_noun]

答案 2 :(得分:0)

您不需要csv包。试试这个:

data = ['tomorrow', 'space', 'Film']
with open('_temp.csv', 'w') as handle:
    handle.write("\n".join(data))