简体   繁体   中英

How to convert a matrix and save to a csv file

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] 

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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