248

I have a long list of lists of the following form ---

a = [[1.2,'abc',3],[1.2,'werew',4],........,[1.4,'qew',2]]

i.e. the values in the list are of different types -- float,int, strings.How do I write it into a csv file so that my output csv file looks like

1.2,abc,3
1.2,werew,4
.
.
.
1.4,qew,2

12 Answers12

421

Python's built-in CSV module can handle this easily:

import csv

with open("output.csv", "wb") as f:
    writer = csv.writer(f)
    writer.writerows(a)

This assumes your list is defined as a, as it is in your question. You can tweak the exact format of the output CSV via the various optional parameters to csv.writer() as documented in the library reference page linked above.

Update for Python 3

import csv

with open("out.csv", "w", newline="") as f:
    writer = csv.writer(f)
    writer.writerows(a)
bucky
  • 392
  • 4
  • 18
Amber
  • 507,862
  • 82
  • 626
  • 550
  • 107
    For Python 3 compatibility, remove the "b" from "wb". – Vlad V May 14 '15 at 11:54
  • 30
    With Python 3 - open('output.csv', 'w', newline=''). I get an extra line if I omit the newline parameter. https://docs.python.org/3/library/csv.html#csv.writer – Spas Jul 02 '15 at 15:50
  • 1
    In python3 I had to use open('output.csv', 'w', newline="") – Tim M Apr 06 '17 at 09:25
  • 1
    WOW that python 3 error is very unhelpful. Thanks @vladV (a bytes-like object is required, not 'str'). It kinda makes sense in hindsight, but not informative of where to look at all. – Rambatino May 25 '18 at 07:30
  • @VladV I think your comment should be mentioned on Amber's answer. – SDG Jun 18 '19 at 10:11
  • If you get error "TypeError: 'newline' is an invalid keyword argument for this function" just remove the newline argument. with open(file_name, "w") as f: – tlalco Feb 06 '20 at 13:20
  • 1
    @tlalco that probably means you're using Python 2, in which case you should use the first code block rather than the second. (It also means you should consider switching to Python 3.) – Amber Feb 07 '20 at 02:53
49

You could use pandas:

In [1]: import pandas as pd

In [2]: a = [[1.2,'abc',3],[1.2,'werew',4],[1.4,'qew',2]]

In [3]: my_df = pd.DataFrame(a)

In [4]: my_df.to_csv('my_csv.csv', index=False, header=False)
Akavall
  • 82,592
  • 51
  • 207
  • 251
  • 4
    I don't think should use `pandas` if built-in library `csv` can do it. – Cloud Sep 28 '17 at 02:19
  • 10
    i like pandas because its powerful – dorbodwolf Dec 10 '17 at 13:24
  • 38
    pandas is powerful, sure, but I'm not going to use a McLaren to drive to the corner store next door. –  Aug 15 '19 at 04:12
  • 1
    Note: For cases, where the list of lists is very large, sequential csv write operations can make the code significantly slower. In such cases using pandas would be wiser. – anjandash Dec 03 '21 at 10:22
  • This was more useful than the previous answer b/c my list was more complex with embedded lists inside the lists of lists. – peetasan Mar 29 '23 at 11:35
42
import csv
with open(file_path, 'a') as outcsv:   
    #configure writer to write standard csv file
    writer = csv.writer(outcsv, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
    writer.writerow(['number', 'text', 'number'])
    for item in list:
        #Write item to outcsv
        writer.writerow([item[0], item[1], item[2]])

official docs: http://docs.python.org/2/library/csv.html

Mike H-R
  • 7,726
  • 5
  • 43
  • 65
Dmitry Zagorulkin
  • 8,370
  • 4
  • 37
  • 60
15

Using csv.writer in my very large list took quite a time. I decided to use pandas, it was faster and more easy to control and understand:

 import pandas

 yourlist = [[...],...,[...]]
 pd = pandas.DataFrame(yourlist)
 pd.to_csv("mylist.csv")

The good part you can change somethings to make a better csv file:

 yourlist = [[...],...,[...]]
 columns = ["abcd","bcde","cdef"] #a csv with 3 columns
 index = [i[0] for i in yourlist] #first element of every list in yourlist
 not_index_list = [i[1:] for i in yourlist]
 pd = pandas.DataFrame(not_index_list, columns = columns, index = index)

 #Now you have a csv with columns and index:
 pd.to_csv("mylist.csv")
Dinidiniz
  • 771
  • 9
  • 15
11

If for whatever reason you wanted to do it manually (without using a module like csv,pandas,numpy etc.):

with open('myfile.csv','w') as f:
    for sublist in mylist:
        for item in sublist:
            f.write(item + ',')
        f.write('\n')

Of course, rolling your own version can be error-prone and inefficient ... that's usually why there's a module for that. But sometimes writing your own can help you understand how they work, and sometimes it's just easier.

tegan
  • 2,125
  • 2
  • 14
  • 17
8

If you don't want to import csv module for that, you can write a list of lists to a csv file using only Python built-ins

with open("output.csv", "w") as f:
    for row in a:
        f.write("%s\n" % ','.join(str(col) for col in row))
fabda01
  • 3,384
  • 2
  • 31
  • 37
6

Ambers's solution also works well for numpy arrays:

from pylab import *
import csv

array_=arange(0,10,1)
list_=[array_,array_*2,array_*3]
with open("output.csv", "wb") as f:
    writer = csv.writer(f)
    writer.writerows(list_)
Semjon Mössinger
  • 1,798
  • 3
  • 22
  • 32
5

How about dumping the list of list into pickle and restoring it with pickle module? It's quite convenient.

>>> import pickle
>>> 
>>> mylist = [1, 'foo', 'bar', {1, 2, 3}, [ [1,4,2,6], [3,6,0,10]]]
>>> with open('mylist', 'wb') as f:
...     pickle.dump(mylist, f) 


>>> with open('mylist', 'rb') as f:
...      mylist = pickle.load(f)
>>> mylist
[1, 'foo', 'bar', {1, 2, 3}, [[1, 4, 2, 6], [3, 6, 0, 10]]]
>>> 
Good Will
  • 1,220
  • 16
  • 10
3

Make sure to indicate lineterinator='\n' when create the writer; otherwise, an extra empty line might be written into file after each data line when data sources are from other csv file...

Here is my solution:

with open('csvfile', 'a') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter='    ',quotechar='|', quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
for i in range(0, len(data)):
    spamwriter.writerow(data[i])
Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
yashi wang
  • 41
  • 1
2

I got an error message when following the examples with a newline parameter in the csv.writer function. The following code worked for me.

 with open(strFileName, "w") as f:
    writer = csv.writer(f, delimiter=',',  quoting=csv.QUOTE_MINIMAL)
    writer.writerows(result)
Jie
  • 1,107
  • 1
  • 14
  • 18
1

In case of exporting lll list of lists of lists to .csv, this will work in Python3:

import csv
with open("output.csv", "w") as f:
    writer = csv.writer(f)
    for element in lll:
        writer.writerows(element)
IAmBotmaker
  • 322
  • 5
  • 15
1

Didn't see even a single answer on this page that includes how to include header as well to create the file. Here is a method incorporating that as well. Method works great with python 3

csv_filename = 'abc.csv'
fieldnames = ['Col_1','Col_2','Col_3','Col_4']
with open(csv_filename, mode='w') as csv_file:
    writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    writer.writeheader()
    for i in my_list:
        writer.writerow({fieldnames[0]: i[0], fieldnames[1]: i[1], fieldnames[2]: i[2],fieldnames[3]: i[3]}) 
PanDe
  • 831
  • 10
  • 21