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