O'Reilly logo

Python: Essential Reference, Third Edition by David Beazley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required