简体   繁体   中英

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','blah@gmail.com'],  
        ['Rahul','Bangalore','blah2@gmail.com'],.....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.

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 .

Easy:

csv_file.writerows(mylist)

(Note the s in writerows() )

These are all tuples of strings? Only 40,000 of them? Have you tried this?

with open('outputfile.csv','w') as csv_file:
    csv_file.write('\n'.join(map(','.join, my_list)+'\n'))

You are still iterating over the list, but doing so using map , which is implemented in Python's C library.

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