как записать несколько записей в csv

#python #csv

#python #csv

Вопрос:

У меня есть переменная ‘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', '']
  

Мне нужно записать каждую из них в виде строки в 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
  

Строка для каждой записи

Комментарии:

1. Что вы пробовали до сих пор?

2. с открытым (‘cleanlists.csv’, ‘w’) как csv-файл: writer = csv.writer(csv-файл) для строки в чистом: writer.writerow(строка)

Ответ №1:

Предполагая, что у вас есть список списков

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

Комментарии:

1. нет, у меня есть строки списков, как показано на скриншоте

Ответ №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])