Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8f1462e52e1797a02c97073eed0b7f92 > files > 842

python-docs-2.6.5-2.5mdv2010.2.i586.rpm

<!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="Content-Type" content="text/html; charset=utf-8" />
    
    <title>14.6. plistlib — Generate and parse Mac OS X .plist files &mdash; Python v2.6.5 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.5',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.5 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.5 documentation" href="../index.html" />
    <link rel="up" title="14. File Formats" href="fileformats.html" />
    <link rel="next" title="15. Cryptographic Services" href="crypto.html" />
    <link rel="prev" title="14.5. xdrlib — Encode and decode XDR data" href="xdrlib.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <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="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="crypto.html" title="15. Cryptographic Services"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="xdrlib.html" title="14.5. xdrlib — Encode and decode XDR data"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.5 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="fileformats.html" accesskey="U">14. File Formats</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-plistlib">
<h1>14.6. <tt class="xref docutils literal"><span class="pre">plistlib</span></tt> &#8212; Generate and parse Mac OS X <tt class="docutils literal"><span class="pre">.plist</span></tt> files<a class="headerlink" href="#module-plistlib" title="Permalink to this headline">¶</a></h1>
<p class="versionchanged">
<span class="versionmodified">Changed in version 2.6: </span>This module was previously only available in the Mac-specific library, it is
now available for all platforms.</p>
<p id="index-497">This module provides an interface for reading and writing the &#8220;property list&#8221;
XML files used mainly by Mac OS X.</p>
<p>The property list (<tt class="docutils literal"><span class="pre">.plist</span></tt>) file format is a simple XML pickle supporting
basic object types, like dictionaries, lists, numbers and strings.  Usually the
top level object is a dictionary.</p>
<p>Values can be strings, integers, floats, booleans, tuples, lists, dictionaries
(but only with string keys), <a title="plistlib.Data" class="reference internal" href="#plistlib.Data"><tt class="xref docutils literal"><span class="pre">Data</span></tt></a> or <a title="datetime.datetime" class="reference external" href="datetime.html#datetime.datetime"><tt class="xref docutils literal"><span class="pre">datetime.datetime</span></tt></a>
objects.  String values (including dictionary keys) may be unicode strings &#8211;
they will be written out as UTF-8.</p>
<p>The <tt class="docutils literal"><span class="pre">&lt;data&gt;</span></tt> plist type is supported through the <a title="plistlib.Data" class="reference internal" href="#plistlib.Data"><tt class="xref docutils literal"><span class="pre">Data</span></tt></a> class.  This is
a thin wrapper around a Python string.  Use <a title="plistlib.Data" class="reference internal" href="#plistlib.Data"><tt class="xref docutils literal"><span class="pre">Data</span></tt></a> if your strings
contain control characters.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference external" href="http://developer.apple.com/documentation/Darwin/Reference/ManPages/man5/plist.5.html">PList manual page</a></dt>
<dd>Apple&#8217;s documentation of the file format.</dd>
</dl>
</div>
<p>This module defines the following functions:</p>
<dl class="function">
<dt id="plistlib.readPlist">
<tt class="descclassname">plistlib.</tt><tt class="descname">readPlist</tt><big>(</big><em>pathOrFile</em><big>)</big><a class="headerlink" href="#plistlib.readPlist" title="Permalink to this definition">¶</a></dt>
<dd><p>Read a plist file. <em>pathOrFile</em> may either be a file name or a (readable)
file object.  Return the unpacked root object (which usually is a
dictionary).</p>
<p>The XML data is parsed using the Expat parser from <a title="An interface to the Expat non-validating XML parser." class="reference external" href="pyexpat.html#module-xml.parsers.expat"><tt class="xref docutils literal"><span class="pre">xml.parsers.expat</span></tt></a>
&#8211; see its documentation for possible exceptions on ill-formed XML.
Unknown elements will simply be ignored by the plist parser.</p>
</dd></dl>

<dl class="function">
<dt id="plistlib.writePlist">
<tt class="descclassname">plistlib.</tt><tt class="descname">writePlist</tt><big>(</big><em>rootObject</em>, <em>pathOrFile</em><big>)</big><a class="headerlink" href="#plistlib.writePlist" title="Permalink to this definition">¶</a></dt>
<dd><p>Write <em>rootObject</em> to a plist file. <em>pathOrFile</em> may either be a file name
or a (writable) file object.</p>
<p>A <a title="exceptions.TypeError" class="reference external" href="exceptions.html#exceptions.TypeError"><tt class="xref docutils literal"><span class="pre">TypeError</span></tt></a> will be raised if the object is of an unsupported type or
a container that contains objects of unsupported types.</p>
</dd></dl>

<dl class="function">
<dt id="plistlib.readPlistFromString">
<tt class="descclassname">plistlib.</tt><tt class="descname">readPlistFromString</tt><big>(</big><em>data</em><big>)</big><a class="headerlink" href="#plistlib.readPlistFromString" title="Permalink to this definition">¶</a></dt>
<dd>Read a plist from a string.  Return the root object.</dd></dl>

<dl class="function">
<dt id="plistlib.writePlistToString">
<tt class="descclassname">plistlib.</tt><tt class="descname">writePlistToString</tt><big>(</big><em>rootObject</em><big>)</big><a class="headerlink" href="#plistlib.writePlistToString" title="Permalink to this definition">¶</a></dt>
<dd>Return <em>rootObject</em> as a plist-formatted string.</dd></dl>

<dl class="function">
<dt id="plistlib.readPlistFromResource">
<tt class="descclassname">plistlib.</tt><tt class="descname">readPlistFromResource</tt><big>(</big><em>path</em><span class="optional">[</span>, <em>restype='plst'</em><span class="optional">[</span>, <em>resid=0</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#plistlib.readPlistFromResource" title="Permalink to this definition">¶</a></dt>
<dd><p>Read a plist from the resource with type <em>restype</em> from the resource fork of
<em>path</em>.  Availability: Mac OS X.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">In Python 3.x, this function has been removed.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="plistlib.writePlistToResource">
<tt class="descclassname">plistlib.</tt><tt class="descname">writePlistToResource</tt><big>(</big><em>rootObject</em>, <em>path</em><span class="optional">[</span>, <em>restype='plst'</em><span class="optional">[</span>, <em>resid=0</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#plistlib.writePlistToResource" title="Permalink to this definition">¶</a></dt>
<dd><p>Write <em>rootObject</em> as a resource with type <em>restype</em> to the resource fork of
<em>path</em>.  Availability: Mac OS X.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">In Python 3.x, this function has been removed.</p>
</div>
</dd></dl>

<p>The following class is available:</p>
<dl class="class">
<dt id="plistlib.Data">
<em class="property">class </em><tt class="descclassname">plistlib.</tt><tt class="descname">Data</tt><big>(</big><em>data</em><big>)</big><a class="headerlink" href="#plistlib.Data" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a &#8220;data&#8221; wrapper object around the string <em>data</em>.  This is used in
functions converting from/to plists to represent the <tt class="docutils literal"><span class="pre">&lt;data&gt;</span></tt> type
available in plists.</p>
<p>It has one attribute, <tt class="xref docutils literal"><span class="pre">data</span></tt>, that can be used to retrieve the Python
string stored in it.</p>
</dd></dl>

<div class="section" id="examples">
<h2>14.6.1. Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2>
<p>Generating a plist:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">pl</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
    <span class="n">aString</span><span class="o">=</span><span class="s">&quot;Doodah&quot;</span><span class="p">,</span>
    <span class="n">aList</span><span class="o">=</span><span class="p">[</span><span class="s">&quot;A&quot;</span><span class="p">,</span> <span class="s">&quot;B&quot;</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mf">32.1</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]],</span>
    <span class="n">aFloat</span> <span class="o">=</span> <span class="mf">0.1</span><span class="p">,</span>
    <span class="n">anInt</span> <span class="o">=</span> <span class="mi">728</span><span class="p">,</span>
    <span class="n">aDict</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span>
        <span class="n">anotherString</span><span class="o">=</span><span class="s">&quot;&lt;hello &amp; hi there!&gt;&quot;</span><span class="p">,</span>
        <span class="n">aUnicodeValue</span><span class="o">=</span><span class="s">u&#39;M</span><span class="se">\xe4</span><span class="s">ssig, Ma</span><span class="se">\xdf</span><span class="s">&#39;</span><span class="p">,</span>
        <span class="n">aTrueValue</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
        <span class="n">aFalseValue</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
    <span class="p">),</span>
    <span class="n">someData</span> <span class="o">=</span> <span class="n">Data</span><span class="p">(</span><span class="s">&quot;&lt;binary gunk&gt;&quot;</span><span class="p">),</span>
    <span class="n">someMoreData</span> <span class="o">=</span> <span class="n">Data</span><span class="p">(</span><span class="s">&quot;&lt;lots of binary gunk&gt;&quot;</span> <span class="o">*</span> <span class="mi">10</span><span class="p">),</span>
    <span class="n">aDate</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">mktime</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">gmtime</span><span class="p">())),</span>
<span class="p">)</span>
<span class="c"># unicode keys are possible, but a little awkward to use:</span>
<span class="n">pl</span><span class="p">[</span><span class="s">u&#39;</span><span class="se">\xc5</span><span class="s">benraa&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&quot;That was a unicode key.&quot;</span>
<span class="n">writePlist</span><span class="p">(</span><span class="n">pl</span><span class="p">,</span> <span class="n">fileName</span><span class="p">)</span>
</pre></div>
</div>
<p>Parsing a plist:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">pl</span> <span class="o">=</span> <span class="n">readPlist</span><span class="p">(</span><span class="n">pathOrFile</span><span class="p">)</span>
<span class="k">print</span> <span class="n">pl</span><span class="p">[</span><span class="s">&quot;aKey&quot;</span><span class="p">]</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="#">14.6. <tt class="docutils literal"><span class="pre">plistlib</span></tt> &#8212; Generate and parse Mac OS X <tt class="docutils literal"><span class="pre">.plist</span></tt> files</a><ul>
<li><a class="reference external" href="#examples">14.6.1. Examples</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="xdrlib.html"
                                  title="previous chapter">14.5. <tt class="docutils literal docutils literal"><span class="pre">xdrlib</span></tt> &#8212; Encode and decode XDR data</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="crypto.html"
                                  title="next chapter">15. Cryptographic Services</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/plistlib.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <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="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="crypto.html" title="15. Cryptographic Services"
             >next</a> |</li>
        <li class="right" >
          <a href="xdrlib.html" title="14.5. xdrlib — Encode and decode XDR data"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.5 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="fileformats.html" >14. File Formats</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2010, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Mar 19, 2010.
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.5.
    </div>

  </body>
</html>