Kerala Cyber Warriors
KCW Uploader V1.1

Path : /usr/share/doc/python2.7-dev/html/library/
File Upload :
Current File : //usr/share/doc/python2.7-dev/html/library/stringio.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>7.5. StringIO — Read and write strings as files &#8212; Python 2.7.18 documentation</title>
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 2.7.18 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="next" title="7.7. textwrap — Text wrapping and filling" href="textwrap.html" />
    <link rel="prev" title="7.4. difflib — Helpers for computing deltas" href="difflib.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="file:///usr/share/doc/python2.7/html/library/stringio.html" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
 
    

  </head><body>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
    This document is for an old version of Python that is <a href="https://devguide.python.org/devcycle/#end-of-life-branches">no longer supported</a>.
    You should install the python3 and python3-doc packages and read the 
    <a href="file:///usr/share/doc/python3-doc/html/library/stringio.html"> Python documentation for the Python3 version packaged in this release</a>.
</div>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="textwrap.html" title="7.7. textwrap — Text wrapping and filling"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="difflib.html" title="7.4. difflib — Helpers for computing deltas"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">Python 2.7.18 documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="strings.html" accesskey="U">7. String Services</a> &#187;</li> 
      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-StringIO">
<span id="stringio-read-and-write-strings-as-files"></span><h1>7.5. <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">StringIO</span></code></a> — Read and write strings as files<a class="headerlink" href="#module-StringIO" title="Permalink to this headline">¶</a></h1>
<p>This module implements a file-like class, <a class="reference internal" href="#StringIO.StringIO" title="StringIO.StringIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">StringIO</span></code></a>, that reads and
writes a string buffer (also known as <em>memory files</em>).  See the description of
file objects for operations (section <a class="reference internal" href="stdtypes.html#bltin-file-objects"><span class="std std-ref">File Objects</span></a>). (For
standard strings, see <a class="reference internal" href="functions.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> and <a class="reference internal" href="functions.html#unicode" title="unicode"><code class="xref py py-class docutils literal notranslate"><span class="pre">unicode</span></code></a>.)</p>
<dl class="class">
<dt id="StringIO.StringIO">
<em class="property">class </em><code class="descclassname">StringIO.</code><code class="descname">StringIO</code><span class="sig-paren">(</span><span class="optional">[</span><em>buffer</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#StringIO.StringIO" title="Permalink to this definition">¶</a></dt>
<dd><p>When a <a class="reference internal" href="#StringIO.StringIO" title="StringIO.StringIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">StringIO</span></code></a> object is created, it can be initialized to an existing
string by passing the string to the constructor. If no string is given, the
<a class="reference internal" href="#StringIO.StringIO" title="StringIO.StringIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">StringIO</span></code></a> will start empty. In both cases, the initial file position
starts at zero.</p>
<p>The <a class="reference internal" href="#StringIO.StringIO" title="StringIO.StringIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">StringIO</span></code></a> object can accept either Unicode or 8-bit strings, but
mixing the two may take some care.  If both are used, 8-bit strings that cannot
be interpreted as 7-bit ASCII (that use the 8th bit) will cause a
<a class="reference internal" href="exceptions.html#exceptions.UnicodeError" title="exceptions.UnicodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeError</span></code></a> to be raised when <a class="reference internal" href="#StringIO.StringIO.getvalue" title="StringIO.StringIO.getvalue"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getvalue()</span></code></a> is called.</p>
</dd></dl>

<p>The following methods of <a class="reference internal" href="#StringIO.StringIO" title="StringIO.StringIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">StringIO</span></code></a> objects require special mention:</p>
<dl class="method">
<dt id="StringIO.StringIO.getvalue">
<code class="descclassname">StringIO.</code><code class="descname">getvalue</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#StringIO.StringIO.getvalue" title="Permalink to this definition">¶</a></dt>
<dd><p>Retrieve the entire contents of the “file” at any time before the
<a class="reference internal" href="#StringIO.StringIO" title="StringIO.StringIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">StringIO</span></code></a> object’s <a class="reference internal" href="#StringIO.StringIO.close" title="StringIO.StringIO.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> method is called.  See the note above
for information about mixing Unicode and 8-bit strings; such mixing can cause
this method to raise <a class="reference internal" href="exceptions.html#exceptions.UnicodeError" title="exceptions.UnicodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeError</span></code></a>.</p>
</dd></dl>

<dl class="method">
<dt id="StringIO.StringIO.close">
<code class="descclassname">StringIO.</code><code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#StringIO.StringIO.close" title="Permalink to this definition">¶</a></dt>
<dd><p>Free the memory buffer.  Attempting to do further operations with a closed
<a class="reference internal" href="#StringIO.StringIO" title="StringIO.StringIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">StringIO</span></code></a> object will raise a <a class="reference internal" href="exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p>
</dd></dl>

<p>Example usage:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">StringIO</span>

<span class="n">output</span> <span class="o">=</span> <span class="n">StringIO</span><span class="o">.</span><span class="n">StringIO</span><span class="p">()</span>
<span class="n">output</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;First line.</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
<span class="nb">print</span> <span class="o">&gt;&gt;</span><span class="n">output</span><span class="p">,</span> <span class="s1">&#39;Second line.&#39;</span>

<span class="c1"># Retrieve file contents -- this will be</span>
<span class="c1"># &#39;First line.\nSecond line.\n&#39;</span>
<span class="n">contents</span> <span class="o">=</span> <span class="n">output</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>

<span class="c1"># Close object and discard memory buffer --</span>
<span class="c1"># .getvalue() will now raise an exception.</span>
<span class="n">output</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="module-cStringIO">
<span id="cstringio-faster-version-of-stringio"></span><h1>7.6. <a class="reference internal" href="#module-cStringIO" title="cStringIO: Faster version of StringIO, but not subclassable."><code class="xref py py-mod docutils literal notranslate"><span class="pre">cStringIO</span></code></a> — Faster version of <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">StringIO</span></code></a><a class="headerlink" href="#module-cStringIO" title="Permalink to this headline">¶</a></h1>
<p>The module <a class="reference internal" href="#module-cStringIO" title="cStringIO: Faster version of StringIO, but not subclassable."><code class="xref py py-mod docutils literal notranslate"><span class="pre">cStringIO</span></code></a> provides an interface similar to that of the
<a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">StringIO</span></code></a> module.  Heavy use of <a class="reference internal" href="#StringIO.StringIO" title="StringIO.StringIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">StringIO.StringIO</span></code></a> objects can be
made more efficient by using the function <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><code class="xref py py-func docutils literal notranslate"><span class="pre">StringIO()</span></code></a> from this module
instead.</p>
<dl class="function">
<dt id="cStringIO.StringIO">
<code class="descclassname">cStringIO.</code><code class="descname">StringIO</code><span class="sig-paren">(</span><span class="optional">[</span><em>s</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#cStringIO.StringIO" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a StringIO-like stream for reading or writing.</p>
<p>Since this is a factory function which returns objects of built-in types,
there’s no way to build your own version using subclassing.  It’s not
possible to set attributes on it.  Use the original <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">StringIO</span></code></a> module in
those cases.</p>
<p>Unlike the <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">StringIO</span></code></a> module, this module is not able to accept Unicode
strings that cannot be encoded as plain ASCII strings.</p>
<p>Another difference from the <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">StringIO</span></code></a> module is that calling
<a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><code class="xref py py-func docutils literal notranslate"><span class="pre">StringIO()</span></code></a> with a string parameter creates a read-only object. Unlike an
object created without a string parameter, it does not have write methods.
These objects are not generally visible.  They turn up in tracebacks as
<code class="xref py py-class docutils literal notranslate"><span class="pre">StringI</span></code> and <code class="xref py py-class docutils literal notranslate"><span class="pre">StringO</span></code>.</p>
</dd></dl>

<p>The following data objects are provided as well:</p>
<dl class="data">
<dt id="cStringIO.InputType">
<code class="descclassname">cStringIO.</code><code class="descname">InputType</code><a class="headerlink" href="#cStringIO.InputType" title="Permalink to this definition">¶</a></dt>
<dd><p>The type object of the objects created by calling <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><code class="xref py py-func docutils literal notranslate"><span class="pre">StringIO()</span></code></a> with a string
parameter.</p>
</dd></dl>

<dl class="data">
<dt id="cStringIO.OutputType">
<code class="descclassname">cStringIO.</code><code class="descname">OutputType</code><a class="headerlink" href="#cStringIO.OutputType" title="Permalink to this definition">¶</a></dt>
<dd><p>The type object of the objects returned by calling <a class="reference internal" href="#module-StringIO" title="StringIO: Read and write strings as if they were files."><code class="xref py py-func docutils literal notranslate"><span class="pre">StringIO()</span></code></a> with no
parameters.</p>
</dd></dl>

<p>There is a C API to the module as well; refer to the module source for  more
information.</p>
<p>Example usage:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">cStringIO</span>

<span class="n">output</span> <span class="o">=</span> <span class="n">cStringIO</span><span class="o">.</span><span class="n">StringIO</span><span class="p">()</span>
<span class="n">output</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;First line.</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
<span class="nb">print</span> <span class="o">&gt;&gt;</span><span class="n">output</span><span class="p">,</span> <span class="s1">&#39;Second line.&#39;</span>

<span class="c1"># Retrieve file contents -- this will be</span>
<span class="c1"># &#39;First line.\nSecond line.\n&#39;</span>
<span class="n">contents</span> <span class="o">=</span> <span class="n">output</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>

<span class="c1"># Close object and discard memory buffer --</span>
<span class="c1"># .getvalue() will now raise an exception.</span>
<span class="n">output</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">7.5. <code class="docutils literal notranslate"><span class="pre">StringIO</span></code> — Read and write strings as files</a></li>
<li><a class="reference internal" href="#module-cStringIO">7.6. <code class="docutils literal notranslate"><span class="pre">cStringIO</span></code> — Faster version of <code class="docutils literal notranslate"><span class="pre">StringIO</span></code></a></li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="difflib.html"
                        title="previous chapter">7.4. <code class="docutils literal notranslate"><span class="pre">difflib</span></code> — Helpers for computing deltas</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="textwrap.html"
                        title="next chapter">7.7. <code class="docutils literal notranslate"><span class="pre">textwrap</span></code> — Text wrapping and filling</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/library/stringio.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="textwrap.html" title="7.7. textwrap — Text wrapping and filling"
             >next</a> |</li>
        <li class="right" >
          <a href="difflib.html" title="7.4. difflib — Helpers for computing deltas"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">Python 2.7.18 documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="strings.html" >7. String Services</a> &#187;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2024, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on November 21, 2024.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.8.5.
    </div>

  </body>
</html>

-=[ KCW uplo4d3r c0ded by cJ_n4p573r ]=-
Ⓒ2017 ҠЄГѦLѦ СүѣЄГ ЩѦГГіѺГՏ