February 2006
Intermediate to advanced
648 pages
14h 53m
English
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.
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 ...