如何在cs中写入多个条目

2024-05-29 05:11:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个变量'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

每个条目对应一行


Tags: runmakeconnectprettygpsgoodappearshappy
2条回答

这似乎奏效了:

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

假设你有一个列表

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

相关问题 更多 >

    热门问题