blob: 15edfba3340728aba908aca9bdc51f22478559fa [file] [log] [blame]
Georg Brandlf6945182008-02-01 11:56:49 +00001.. XXX this whole file is outdated
Georg Brandl116aa622007-08-15 14:28:22 +00002
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
10This module implements a file-like class, :class:`StringIO`, that reads and
11writes a string buffer (also known as *memory files*). See the description of
Thomas Wouters47b49bf2007-08-30 22:15:33 +000012file objects for operations (section :ref:`bltin-file-objects`). (For
Georg Brandlf6945182008-02-01 11:56:49 +000013standard strings, see :class:`str`.)
Georg Brandl116aa622007-08-15 14:28:22 +000014
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 Brandl116aa622007-08-15 14:28:22 +000023The 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 Brandlf6945182008-02-01 11:56:49 +000029 :class:`StringIO` object's :meth:`close` method is called.
Georg Brandl116aa622007-08-15 14:28:22 +000030
31
32.. method:: StringIO.close()
33
34 Free the memory buffer.
35
36Example usage::
37
38 import StringIO
39
40 output = StringIO.StringIO()
41 output.write('First line.\n')
Collin Winterc79461b2007-09-01 23:34:30 +000042 print('Second line.', file=output)
Georg Brandl116aa622007-08-15 14:28:22 +000043
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
62The module :mod:`cStringIO` provides an interface similar to that of the
63:mod:`StringIO` module. Heavy use of :class:`StringIO.StringIO` objects can be
64made more efficient by using the function :func:`StringIO` from this module
65instead.
66
67Since this module provides a factory function which returns objects of built-in
68types, there's no way to build your own version using subclassing. Use the
69original :mod:`StringIO` module in that case.
70
71Unlike the memory files implemented by the :mod:`StringIO` module, those
Georg Brandlf6945182008-02-01 11:56:49 +000072provided by this module are not able to accept strings that cannot be
73encoded in plain ASCII.
Georg Brandl116aa622007-08-15 14:28:22 +000074
Georg Brandlf6945182008-02-01 11:56:49 +000075Calling :func:`StringIO` with a string parameter populates
76the object with the buffer representation of the string, instead of
Georg Brandl116aa622007-08-15 14:28:22 +000077encoding the string.
78
79Another difference from the :mod:`StringIO` module is that calling
80:func:`StringIO` with a string parameter creates a read-only object. Unlike an
81object created without a string parameter, it does not have write methods.
82These objects are not generally visible. They turn up in tracebacks as
83:class:`StringI` and :class:`StringO`.
84
85The 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
99There is a C API to the module as well; refer to the module source for more
100information.
101
102Example usage::
103
104 import cStringIO
105
106 output = cStringIO.StringIO()
107 output.write('First line.\n')
Collin Winterc79461b2007-09-01 23:34:30 +0000108 print('Second line.', file=output)
Georg Brandl116aa622007-08-15 14:28:22 +0000109
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