| Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 |  | 
|  | 2 | :mod:`StringIO` --- Read and write strings as files | 
|  | 3 | =================================================== | 
|  | 4 |  | 
|  | 5 | .. module:: StringIO | 
|  | 6 | :synopsis: Read and write strings as if they were files. | 
|  | 7 |  | 
|  | 8 |  | 
|  | 9 | This module implements a file-like class, :class:`StringIO`, that reads and | 
|  | 10 | writes a string buffer (also known as *memory files*).  See the description of | 
|  | 11 | file objects for operations (section :ref:`bltin-file-objects`). | 
|  | 12 |  | 
|  | 13 |  | 
|  | 14 | .. class:: StringIO([buffer]) | 
|  | 15 |  | 
|  | 16 | When a :class:`StringIO` object is created, it can be initialized to an existing | 
|  | 17 | string by passing the string to the constructor. If no string is given, the | 
|  | 18 | :class:`StringIO` will start empty. In both cases, the initial file position | 
|  | 19 | starts at zero. | 
|  | 20 |  | 
|  | 21 | The :class:`StringIO` object can accept either Unicode or 8-bit strings, but | 
|  | 22 | mixing the two may take some care.  If both are used, 8-bit strings that cannot | 
|  | 23 | be interpreted as 7-bit ASCII (that use the 8th bit) will cause a | 
|  | 24 | :exc:`UnicodeError` to be raised when :meth:`getvalue` is called. | 
|  | 25 |  | 
|  | 26 | The following methods of :class:`StringIO` objects require special mention: | 
|  | 27 |  | 
|  | 28 |  | 
|  | 29 | .. method:: StringIO.getvalue() | 
|  | 30 |  | 
|  | 31 | Retrieve the entire contents of the "file" at any time before the | 
|  | 32 | :class:`StringIO` object's :meth:`close` method is called.  See the note above | 
|  | 33 | for information about mixing Unicode and 8-bit strings; such mixing can cause | 
|  | 34 | this method to raise :exc:`UnicodeError`. | 
|  | 35 |  | 
|  | 36 |  | 
|  | 37 | .. method:: StringIO.close() | 
|  | 38 |  | 
|  | 39 | Free the memory buffer. | 
|  | 40 |  | 
|  | 41 | Example usage:: | 
|  | 42 |  | 
|  | 43 | import StringIO | 
|  | 44 |  | 
|  | 45 | output = StringIO.StringIO() | 
|  | 46 | output.write('First line.\n') | 
|  | 47 | print >>output, 'Second line.' | 
|  | 48 |  | 
|  | 49 | # Retrieve file contents -- this will be | 
|  | 50 | # 'First line.\nSecond line.\n' | 
|  | 51 | contents = output.getvalue() | 
|  | 52 |  | 
|  | 53 | # Close object and discard memory buffer -- | 
|  | 54 | # .getvalue() will now raise an exception. | 
|  | 55 | output.close() | 
|  | 56 |  | 
|  | 57 |  | 
|  | 58 | :mod:`cStringIO` --- Faster version of :mod:`StringIO` | 
|  | 59 | ====================================================== | 
|  | 60 |  | 
|  | 61 | .. module:: cStringIO | 
|  | 62 | :synopsis: Faster version of StringIO, but not subclassable. | 
|  | 63 | .. moduleauthor:: Jim Fulton <jim@zope.com> | 
|  | 64 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> | 
|  | 65 |  | 
|  | 66 |  | 
|  | 67 | The module :mod:`cStringIO` provides an interface similar to that of the | 
|  | 68 | :mod:`StringIO` module.  Heavy use of :class:`StringIO.StringIO` objects can be | 
|  | 69 | made more efficient by using the function :func:`StringIO` from this module | 
|  | 70 | instead. | 
|  | 71 |  | 
|  | 72 | Since this module provides a factory function which returns objects of built-in | 
|  | 73 | types, there's no way to build your own version using subclassing.  Use the | 
|  | 74 | original :mod:`StringIO` module in that case. | 
|  | 75 |  | 
|  | 76 | Unlike the memory files implemented by the :mod:`StringIO` module, those | 
|  | 77 | provided by this module are not able to accept Unicode strings that cannot be | 
|  | 78 | encoded as plain ASCII strings. | 
|  | 79 |  | 
|  | 80 | Calling :func:`StringIO` with a Unicode string parameter populates | 
|  | 81 | the object with the buffer representation of the Unicode string, instead of | 
|  | 82 | encoding the string. | 
|  | 83 |  | 
|  | 84 | Another difference from the :mod:`StringIO` module is that calling | 
|  | 85 | :func:`StringIO` with a string parameter creates a read-only object. Unlike an | 
|  | 86 | object created without a string parameter, it does not have write methods. | 
|  | 87 | These objects are not generally visible.  They turn up in tracebacks as | 
|  | 88 | :class:`StringI` and :class:`StringO`. | 
|  | 89 |  | 
|  | 90 | The following data objects are provided as well: | 
|  | 91 |  | 
|  | 92 |  | 
|  | 93 | .. data:: InputType | 
|  | 94 |  | 
|  | 95 | The type object of the objects created by calling :func:`StringIO` with a string | 
|  | 96 | parameter. | 
|  | 97 |  | 
|  | 98 |  | 
|  | 99 | .. data:: OutputType | 
|  | 100 |  | 
|  | 101 | The type object of the objects returned by calling :func:`StringIO` with no | 
|  | 102 | parameters. | 
|  | 103 |  | 
|  | 104 | There is a C API to the module as well; refer to the module source for  more | 
|  | 105 | information. | 
|  | 106 |  | 
|  | 107 | Example usage:: | 
|  | 108 |  | 
|  | 109 | import cStringIO | 
|  | 110 |  | 
|  | 111 | output = cStringIO.StringIO() | 
|  | 112 | output.write('First line.\n') | 
|  | 113 | print >>output, 'Second line.' | 
|  | 114 |  | 
|  | 115 | # Retrieve file contents -- this will be | 
|  | 116 | # 'First line.\nSecond line.\n' | 
|  | 117 | contents = output.getvalue() | 
|  | 118 |  | 
|  | 119 | # Close object and discard memory buffer -- | 
|  | 120 | # .getvalue() will now raise an exception. | 
|  | 121 | output.close() | 
|  | 122 |  |