Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a list to csv file without looping in python

I have a list of list of lists that I need to write to a csv file.

mylist = [['Siddharth','Bangalore','[email protected]'],  
        ['Rahul','Bangalore','[email protected]'],.....and so on]  

This list is usually some 20,000 to 40,000 long.
So, right now, the only way to write them to a csv file is to iterate over the list and write:

fileObj = open("/home/siddharth/sample.csv", "wb")
csv_file = csv.writer(fileObj)  
for item in mylist:  
    csv_file.writerow(item)  

So, I just to wanted to know, is there a way to write such a list of lists to csv, without iterating over each item in the list, eg. like using StringIO etc.
Or, can anyone give some tip/hint, so that I can create my own implementation.

like image 963
Siddharth Srivastava Avatar asked Dec 26 '22 12:12

Siddharth Srivastava


1 Answers

There is a writerows method which will add all the rows in an iterable:

csv_file.writerows(the_list)

Whatever you do, there will always be a loop somewhere (either in your code or in the Python library implementation). There is no way around it as you need to look at each item in the list so that you can write them to the file.

In case you're worried about the performance of writing each line to the file separately: Python uses buffered I/O by default, so even if you write the list items one by one in a loop, they won't necessarily be written like that to the file. They will be written in chunks whenever the buffer fills up, which will have better performance. If needed, you can explicitly control the buffer size by using the buffering parameter of open.

like image 59
interjay Avatar answered Jan 31 '23 04:01

interjay