Strings and Files

The StringIO and cStringIO modules

  • Provide a file-like object that reads/writes from a string buffer
  • Example:
     import StringIO
     f = StringIO.StringIO()
     f.write("Hello World\n")
     ...
     s = f.getvalue()           # Get saved string value

Notes

  • StringIO objects support most of the normal file operations
  • cStringIO is implemented in C and is significantly faster.
  • StringIO is implemented in Python and can be subclassed.
<<< O'Reilly OSCON 2000, Advanced Python Programming, Slide 43
July 17, 2000, beazley@cs.uchicago.edu
>>>