简体   繁体   中英

python file-like buffer object

I've written a buffer class that provides a File-like interface with read , write , seek , tell , flush methods to a simple string in memory. Of course it is incomplete (eg I didn't write readline ). It's purpose is to be filled by a background thread from some external data source, but let a user treat it like a file. I'd expect it to contain a relatively small amount of data (maybe 50K max)

Is there a better way to do this instead of writing it from scratch?

You can use the standard Python modules StringIO or cStringIO to obtain an in-memory buffer which implements the file interface .

cStringIO is implemented in C, and will be faster, so you should use that version if possible.

If you're using Python 3 you should use the io.StringIO instead of StringIO and io.BytesIO instead of cStringIO .

我想你可能正在寻找StringIO

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