将列表写入csv文件而不在python中循环 [英] Write a list to csv file without looping in python

查看:52
本文介绍了将列表写入csv文件而不在python中循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要写入 csv 文件的列表列表.

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]  

这个列表通常有 20,000 到 40,000 长.
所以,现在,将它们写入 csv 文件的唯一方法是遍历列表并写入:

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)  

所以,我只是想知道,有没有一种方法可以将这样的列表列表写入 csv,而无需遍历列表中的每个项目,例如.比如使用 StringIO
或者,任何人都可以提供一些提示/提示,以便我可以创建自己的实现.

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.

推荐答案

有一个 writerows 方法,它将添加可迭代中的所有行:

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

csv_file.writerows(the_list)

无论你做什么,总会有一个循环(在你的代码或 Python 库实现中).没有办法解决它,因为您需要查看列表中的每个项目,以便将它们写入文件.

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.

如果您担心将每一行单独写入文件的性能:Python 默认使用缓冲 I/O,因此即使您在循环中逐个写入列表项,它们也不一定像这样写到文件中.每当缓冲区填满时,它们都会以块的形式写入,这将具有更好的性能.如果需要,您可以使用 buffering 参数显式控制缓冲区大小"noreferrer">打开.

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.

这篇关于将列表写入csv文件而不在python中循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆