简体   繁体   English

如何在csv中写入多个条目

[英]how to write multiple entries in csv

I have a variable 'clean' that holds these entries: 我有一个变量'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', '']

I need to write each one of them as a row in a csv file without the '[,'']' towards the end. 我需要将它们中的每一个写成csv文件中的一行,而不是'[,'']'到最后。

Hence in my csv the sample output be: 因此,在我的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

A row for each entry 每个条目一行

Assuming you have a list of lists 假设您有一个清单清单

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

This seems to work: 这似乎有效:

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM