开发者

Storing strings somewhere then writing out at once

I read that file I/O is slow and was looking at my own code that wrote to a file and found that I'm constantly calling write write write everywhere even for little things like a comma.

outFile.write(",")

So I figured maybe it'd be better if I stored all my characters somewhere (like, I don't know, 1 KB of characters or something before writing out).

How should I implement this? Do I just take a li开发者_运维技巧st and start appending strings to it, then do a join and write the resulting string out? Or maybe there's a better approach to it.


Take a look at the io module's documentation for information on buffered I/O. As S.Lott pointed out, there is already a buffer built into much of Python's built-in I/O objects, so you probably are already using it without your knowledge.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜