逐行将列表项写入csv

时间:2016-12-07 17:19:44

标签: python csv

我有一个包含大量网址的列表,我想逐行写入csv。

e.g。

links = ['http://www.google.com','http://www.google.com','http://www.google.com','http://www.google.com','http://www.google.com']

for word in links:
    f = open('links.csv', 'wb')
    csv.writer(f, lineterminator='\n').writerows([word])
    f.close()

但是这会把它们全部放在他们自己的专栏中:

http://www.google.com,'http://www.google.com',,'http://www.google.com',,'http://www.google.com',etc

(例如,在Excel中,它们占据单元格A1,B1,C1,D1等)

而不是逐行下方,即单元格A1,A2,A3,A4,A5 ......等。

怎么了?

1 个答案:

答案 0 :(得分:1)

这正是你要求它做的事情。要完成你想做的事我会做:

links = ['http://www.google.com','http://www.google.com','http://www.google.com','http://www.google.com','http://www.google.com']
with open("links.csv", "wb") as f:
    f.write("\n".join(links))