如何在csv中写入多个条目

时间:2019-03-17 02:57:32

标签: python csv

我有一个变量'clean',其中包含以下条目:

enter image description here

['connect - appears cant lose make pretty pro make compared made tracked navigate click kept trail downloaded', '']
['gps - hope happy appears entire reading good start eg negative crashed happens save expect certain drain', '']
['app - nt go see relate pervious', '']
['matter - go run set big high kill', '']
['accuracy - average show give found nice free overall remove need huge lose record endomondo web switched', '']
['track - app nt fine', '']
['workout - include right little statistic old run high traveled need longerR happy appears cant biggestS exact', '']
['wish - monthly provide weekly', '']
['interest - enjoyed improves placed consider disabled unfit organize tofix tab suppose overreach cool separate brilliant uninstalling', '']
['google - based next average happy know thought google cool hard worked fit stats metric negative looked', '']
['summary - connect acquire issue built erratic wait pressed incomplete buy external occasional initiated filled returned partial', '']
['talk - easy track give take found whole set setting free slow high nexus pretty travelled come', '']
['phone - perfect runtastic important light repeated replace surprised vague walk thought sensor apps bring measuring laggy', '']
['minute - good keep intuitive become', '']
['run - open much take future difficult', '']
['dataS - low external ant added loses android google fit compatible reported third potential samsung wireless general', '']

我需要在csv文件中将它们每个都写成一行,并且结尾不要有'[,'']'。

因此在我的csv中,示例输出为:

connect - appears cant lose make pretty pro make compared made tracked navigate click kept trail downloaded
gps - hope happy appears entire reading good start eg negative crashed happens save expect certain drain
gps - hope happy appears entire reading good start eg negative crashed happens save expect certain drain
matter - go run set big high kill
accuracy - average show give found nice free overall remove need huge lose record endomondo web switched

每个条目一行

2 个答案:

答案 0 :(得分:0)

假设您有一个列表列表

clean = [['element', ''], ['element 2', '']]
file = open('filename.csv', 'w')
for element in clean:
    file.write(element[0]+'\n')
file.close()

答案 1 :(得分:0)

这似乎可行:

clean = [
    ['connect - appears cant lose make pretty pro make compared made tracked navigate click kept trail downloaded', ''],
    ['gps - hope happy appears entire reading good start eg negative crashed happens save expect certain drain', ''],
    ['app - nt go see relate pervious', ''],
    ['matter - go run set big high kill', ''],
    ['accuracy - average show give found nice free overall remove need huge lose record endomondo web switched', ''],
    ['track - app nt fine', ''],
    ['workout - include right little statistic old run high traveled need longerR happy appears cant biggestS exact', ''],
    ['wish - monthly provide weekly', ''],
    ['interest - enjoyed improves placed consider disabled unfit organize tofix tab suppose overreach cool separate brilliant uninstalling', ''],
    ['google - based next average happy know thought google cool hard worked fit stats metric negative looked', ''],
    ['summary - connect acquire issue built erratic wait pressed incomplete buy external occasional initiated filled returned partial', ''],
    ['talk - easy track give take found whole set setting free slow high nexus pretty travelled come', ''],
    ['phone - perfect runtastic important light repeated replace surprised vague walk thought sensor apps bring measuring laggy', ''],
    ['minute - good keep intuitive become', ''],
    ['run - open much take future difficult', ''],
    ['dataS - low external ant added loses android google fit compatible reported third potential samsung wireless general', ''],
]

import csv

filename = 'clean.csv'
with open(filename, 'w', newline='') as file:
     writer = csv.writer(file)
     for row in clean:
         writer.writerow(row[:-1])