Skip to Content
Python: Essential Reference, Third Edition
book

Python: Essential Reference, Third Edition

by David Beazley
February 2006
Intermediate to advanced content levelIntermediate to advanced
648 pages
14h 53m
English
Sams
Content preview from Python: Essential Reference, Third Edition

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python: Essential Reference

Python: Essential Reference

David M. Beazley

Publisher Resources

ISBN: 0672328623Purchase book