StringIO and cStringIO

The StringIO and cStringIO modules define an object that behaves like a file but reads and writes data from a string buffer.

The following creates a new StringIO object, where s is the initial value (by default, the empty string):

StringIO([s])

A StringIO object supports all the standard file operations—read(), write(), and so on—as well as the following methods:

s.getvalue()

Returns the contents of the string buffer before close() is called.

s.close()

Releases the memory buffer.

Notes

  • The StringIO module defines StringIO as a Python class. cStringIO defines it as an extension type (implemented in C) and provides significantly faster performance.

  • If an initial argument, s, is given to cStringIO.StringIO(s), the resulting ...

Get Python: Essential Reference, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.