source: vendor/python/2.5/Doc/lib/libstringio.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

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