-1

I have some data (1003 rows, 4 columns) in format as seen below, and I want to save them in a csv file:

Current format:

[['likes_count', 'context', 'category', 'object'], ['1', 1, 0, 4], ['0', 1, 0, 4], ['1', 1, 1, 4], ['1', 1, 0, 4], ['1', 0, 0, 4], ['1', 1, 1, 4], ['1', 1, 1, 4], ['1', 1, 1, 4], ['1', 1, 1, 1], ['1', 1, 1, 1], ['1', 0, 0, 4], ['1', 0, 0, 4], ['1', 0, 1, 4], ['1', 0, 0, 4], ['1', 0, 0, 4], ['1', 0, 1, 1], ['1', 1, 2, 4], ['1', 0, 1, 4], ['1', 0, 1, 4], ['1', 0, 1, 4], ['1', 0, 0, 4], ['1', 0, 2, 1], ['1', 0, 0, 4], ['1', 0, 2, 1], ['1', 1, 2, 1], ['1', 1, 2, 1], ['1', 0, 1, 4], ['1', 0, 1, 4], ['13', 0, 1, 4], ['1', 0, 1, 4], ['2', 1, 2, 4], ['1', 1, 2, 4], [...] etc

Desirable CSV fromat:

'likes_count', 'context', 'category', 'object'
['1', 1, 0, 4] 
['1', 1, 0, 4]
['1', 1, 1, 4]
['1', 1, 0, 4]
['1', 0, 0, 4] 
hpaulj
  • 175,871
  • 13
  • 170
  • 282
  • Does this answer your question? [Writing a Python list of lists to a csv file](https://stackoverflow.com/questions/14037540/writing-a-python-list-of-lists-to-a-csv-file) – Pranav Hosangadi May 06 '21 at 15:43
  • Welcome to Stack Overflow! Please take the [tour], read [what's on-topic here](/help/on-topic), [ask], and the [question checklist](//meta.stackoverflow.com/q/260648/843953), and remember that [asking on Stack Overflow is not a substitute for doing your own research.](//meta.stackoverflow.com/a/261593/843953) – Pranav Hosangadi May 06 '21 at 15:44

1 Answers1

0

It's really as simple as:

import csv

with open('output.csv','w') as f:
    writer = csv.writer(f)
    writer.writerows(my_list)

Where my_list is your list. I think you could've found this everywhere on the internet/on SO. See here, here, etc...

Sandertjuhh
  • 627
  • 5
  • 15