gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2117 lines
161KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="codecs — Codec registry and base classes" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/codecs.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/codecs.py This module defines base classes for standard Python codecs (encoders and decoders) and provides access to the internal Python codec registry, which manages the codec and..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/codecs.py This module defines base classes for standard Python codecs (encoders and decoders) and provides access to the internal Python codec registry, which manages the codec and..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>codecs — Codec registry and base classes &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="Data Types" href="datatypes.html" />
  33. <link rel="prev" title="struct — Interpret bytes as packed binary data" href="struct.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/codecs.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">codecs</span></code> — Codec registry and base classes</a><ul>
  86. <li><a class="reference internal" href="#codec-base-classes">Codec Base Classes</a><ul>
  87. <li><a class="reference internal" href="#error-handlers">Error Handlers</a></li>
  88. <li><a class="reference internal" href="#stateless-encoding-and-decoding">Stateless Encoding and Decoding</a></li>
  89. <li><a class="reference internal" href="#incremental-encoding-and-decoding">Incremental Encoding and Decoding</a><ul>
  90. <li><a class="reference internal" href="#incrementalencoder-objects">IncrementalEncoder Objects</a></li>
  91. <li><a class="reference internal" href="#incrementaldecoder-objects">IncrementalDecoder Objects</a></li>
  92. </ul>
  93. </li>
  94. <li><a class="reference internal" href="#stream-encoding-and-decoding">Stream Encoding and Decoding</a><ul>
  95. <li><a class="reference internal" href="#streamwriter-objects">StreamWriter Objects</a></li>
  96. <li><a class="reference internal" href="#streamreader-objects">StreamReader Objects</a></li>
  97. <li><a class="reference internal" href="#streamreaderwriter-objects">StreamReaderWriter Objects</a></li>
  98. <li><a class="reference internal" href="#streamrecoder-objects">StreamRecoder Objects</a></li>
  99. </ul>
  100. </li>
  101. </ul>
  102. </li>
  103. <li><a class="reference internal" href="#encodings-and-unicode">Encodings and Unicode</a></li>
  104. <li><a class="reference internal" href="#standard-encodings">Standard Encodings</a></li>
  105. <li><a class="reference internal" href="#python-specific-encodings">Python Specific Encodings</a><ul>
  106. <li><a class="reference internal" href="#text-encodings">Text Encodings</a></li>
  107. <li><a class="reference internal" href="#binary-transforms">Binary Transforms</a></li>
  108. <li><a class="reference internal" href="#text-transforms">Text Transforms</a></li>
  109. </ul>
  110. </li>
  111. <li><a class="reference internal" href="#module-encodings.idna"><code class="xref py py-mod docutils literal notranslate"><span class="pre">encodings.idna</span></code> — Internationalized Domain Names in Applications</a></li>
  112. <li><a class="reference internal" href="#module-encodings.mbcs"><code class="xref py py-mod docutils literal notranslate"><span class="pre">encodings.mbcs</span></code> — Windows ANSI codepage</a></li>
  113. <li><a class="reference internal" href="#module-encodings.utf_8_sig"><code class="xref py py-mod docutils literal notranslate"><span class="pre">encodings.utf_8_sig</span></code> — UTF-8 codec with BOM signature</a></li>
  114. </ul>
  115. </li>
  116. </ul>
  117. </div>
  118. <div>
  119. <h4>Previous topic</h4>
  120. <p class="topless"><a href="struct.html"
  121. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code> — Interpret bytes as packed binary data</a></p>
  122. </div>
  123. <div>
  124. <h4>Next topic</h4>
  125. <p class="topless"><a href="datatypes.html"
  126. title="next chapter">Data Types</a></p>
  127. </div>
  128. <div role="note" aria-label="source link">
  129. <h3>This Page</h3>
  130. <ul class="this-page-menu">
  131. <li><a href="../bugs.html">Report a Bug</a></li>
  132. <li>
  133. <a href="https://github.com/python/cpython/blob/main/Doc/library/codecs.rst"
  134. rel="nofollow">Show Source
  135. </a>
  136. </li>
  137. </ul>
  138. </div>
  139. </nav>
  140. </div>
  141. </div>
  142. <div class="related" role="navigation" aria-label="related navigation">
  143. <h3>Navigation</h3>
  144. <ul>
  145. <li class="right" style="margin-right: 10px">
  146. <a href="../genindex.html" title="General Index"
  147. accesskey="I">index</a></li>
  148. <li class="right" >
  149. <a href="../py-modindex.html" title="Python Module Index"
  150. >modules</a> |</li>
  151. <li class="right" >
  152. <a href="datatypes.html" title="Data Types"
  153. accesskey="N">next</a> |</li>
  154. <li class="right" >
  155. <a href="struct.html" title="struct — Interpret bytes as packed binary data"
  156. accesskey="P">previous</a> |</li>
  157. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  158. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  159. <li class="switchers">
  160. <div class="language_switcher_placeholder"></div>
  161. <div class="version_switcher_placeholder"></div>
  162. </li>
  163. <li>
  164. </li>
  165. <li id="cpython-language-and-version">
  166. <a href="../index.html">3.12.3 Documentation</a> &#187;
  167. </li>
  168. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  169. <li class="nav-item nav-item-2"><a href="binary.html" accesskey="U">Binary Data Services</a> &#187;</li>
  170. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">codecs</span></code> — Codec registry and base classes</a></li>
  171. <li class="right">
  172. <div class="inline-search" role="search">
  173. <form class="inline-search" action="../search.html" method="get">
  174. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  175. <input type="submit" value="Go" />
  176. </form>
  177. </div>
  178. |
  179. </li>
  180. <li class="right">
  181. <label class="theme-selector-label">
  182. Theme
  183. <select class="theme-selector" oninput="activateTheme(this.value)">
  184. <option value="auto" selected>Auto</option>
  185. <option value="light">Light</option>
  186. <option value="dark">Dark</option>
  187. </select>
  188. </label> |</li>
  189. </ul>
  190. </div>
  191. <div class="document">
  192. <div class="documentwrapper">
  193. <div class="bodywrapper">
  194. <div class="body" role="main">
  195. <section id="module-codecs">
  196. <span id="codecs-codec-registry-and-base-classes"></span><h1><a class="reference internal" href="#module-codecs" title="codecs: Encode and decode data and streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">codecs</span></code></a> — Codec registry and base classes<a class="headerlink" href="#module-codecs" title="Link to this heading">¶</a></h1>
  197. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/codecs.py">Lib/codecs.py</a></p>
  198. <hr class="docutils" id="index-0" />
  199. <p>This module defines base classes for standard Python codecs (encoders and
  200. decoders) and provides access to the internal Python codec registry, which
  201. manages the codec and error handling lookup process. Most standard codecs
  202. are <a class="reference internal" href="../glossary.html#term-text-encoding"><span class="xref std std-term">text encodings</span></a>, which encode text to bytes (and
  203. decode bytes to text), but there are also codecs provided that encode text to
  204. text, and bytes to bytes. Custom codecs may encode and decode between arbitrary
  205. types, but some module features are restricted to be used specifically with
  206. <a class="reference internal" href="../glossary.html#term-text-encoding"><span class="xref std std-term">text encodings</span></a> or with codecs that encode to
  207. <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
  208. <p>The module defines the following functions for encoding and decoding with
  209. any codec:</p>
  210. <dl class="py function">
  211. <dt class="sig sig-object py" id="codecs.encode">
  212. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">encode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'utf-8'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'strict'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.encode" title="Link to this definition">¶</a></dt>
  213. <dd><p>Encodes <em>obj</em> using the codec registered for <em>encoding</em>.</p>
  214. <p><em>Errors</em> may be given to set the desired error handling scheme. The
  215. default error handler is <code class="docutils literal notranslate"><span class="pre">'strict'</span></code> meaning that encoding errors raise
  216. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> (or a more codec specific subclass, such as
  217. <a class="reference internal" href="exceptions.html#UnicodeEncodeError" title="UnicodeEncodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeEncodeError</span></code></a>). Refer to <a class="reference internal" href="#codec-base-classes"><span class="std std-ref">Codec Base Classes</span></a> for more
  218. information on codec error handling.</p>
  219. </dd></dl>
  220. <dl class="py function">
  221. <dt class="sig sig-object py" id="codecs.decode">
  222. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">decode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'utf-8'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'strict'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.decode" title="Link to this definition">¶</a></dt>
  223. <dd><p>Decodes <em>obj</em> using the codec registered for <em>encoding</em>.</p>
  224. <p><em>Errors</em> may be given to set the desired error handling scheme. The
  225. default error handler is <code class="docutils literal notranslate"><span class="pre">'strict'</span></code> meaning that decoding errors raise
  226. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> (or a more codec specific subclass, such as
  227. <a class="reference internal" href="exceptions.html#UnicodeDecodeError" title="UnicodeDecodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeDecodeError</span></code></a>). Refer to <a class="reference internal" href="#codec-base-classes"><span class="std std-ref">Codec Base Classes</span></a> for more
  228. information on codec error handling.</p>
  229. </dd></dl>
  230. <p>The full details for each codec can also be looked up directly:</p>
  231. <dl class="py function">
  232. <dt class="sig sig-object py" id="codecs.lookup">
  233. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">lookup</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.lookup" title="Link to this definition">¶</a></dt>
  234. <dd><p>Looks up the codec info in the Python codec registry and returns a
  235. <a class="reference internal" href="#codecs.CodecInfo" title="codecs.CodecInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">CodecInfo</span></code></a> object as defined below.</p>
  236. <p>Encodings are first looked up in the registry’s cache. If not found, the list of
  237. registered search functions is scanned. If no <a class="reference internal" href="#codecs.CodecInfo" title="codecs.CodecInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">CodecInfo</span></code></a> object is
  238. found, a <a class="reference internal" href="exceptions.html#LookupError" title="LookupError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">LookupError</span></code></a> is raised. Otherwise, the <a class="reference internal" href="#codecs.CodecInfo" title="codecs.CodecInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">CodecInfo</span></code></a> object
  239. is stored in the cache and returned to the caller.</p>
  240. </dd></dl>
  241. <dl class="py class">
  242. <dt class="sig sig-object py" id="codecs.CodecInfo">
  243. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">CodecInfo</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">encode</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">decode</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">streamreader</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">streamwriter</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">incrementalencoder</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">incrementaldecoder</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.CodecInfo" title="Link to this definition">¶</a></dt>
  244. <dd><p>Codec details when looking up the codec registry. The constructor
  245. arguments are stored in attributes of the same name:</p>
  246. <dl class="py attribute">
  247. <dt class="sig sig-object py" id="codecs.CodecInfo.name">
  248. <span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#codecs.CodecInfo.name" title="Link to this definition">¶</a></dt>
  249. <dd><p>The name of the encoding.</p>
  250. </dd></dl>
  251. <dl class="py attribute">
  252. <dt class="sig sig-object py" id="codecs.CodecInfo.encode">
  253. <span class="sig-name descname"><span class="pre">encode</span></span><a class="headerlink" href="#codecs.CodecInfo.encode" title="Link to this definition">¶</a></dt>
  254. <dt class="sig sig-object py" id="codecs.CodecInfo.decode">
  255. <span class="sig-name descname"><span class="pre">decode</span></span><a class="headerlink" href="#codecs.CodecInfo.decode" title="Link to this definition">¶</a></dt>
  256. <dd><p>The stateless encoding and decoding functions. These must be
  257. functions or methods which have the same interface as
  258. the <a class="reference internal" href="#codecs.Codec.encode" title="codecs.Codec.encode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">encode()</span></code></a> and <a class="reference internal" href="#codecs.Codec.decode" title="codecs.Codec.decode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decode()</span></code></a> methods of Codec
  259. instances (see <a class="reference internal" href="#codec-objects"><span class="std std-ref">Codec Interface</span></a>).
  260. The functions or methods are expected to work in a stateless mode.</p>
  261. </dd></dl>
  262. <dl class="py attribute">
  263. <dt class="sig sig-object py" id="codecs.CodecInfo.incrementalencoder">
  264. <span class="sig-name descname"><span class="pre">incrementalencoder</span></span><a class="headerlink" href="#codecs.CodecInfo.incrementalencoder" title="Link to this definition">¶</a></dt>
  265. <dt class="sig sig-object py" id="codecs.CodecInfo.incrementaldecoder">
  266. <span class="sig-name descname"><span class="pre">incrementaldecoder</span></span><a class="headerlink" href="#codecs.CodecInfo.incrementaldecoder" title="Link to this definition">¶</a></dt>
  267. <dd><p>Incremental encoder and decoder classes or factory functions.
  268. These have to provide the interface defined by the base classes
  269. <a class="reference internal" href="#codecs.IncrementalEncoder" title="codecs.IncrementalEncoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">IncrementalEncoder</span></code></a> and <a class="reference internal" href="#codecs.IncrementalDecoder" title="codecs.IncrementalDecoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">IncrementalDecoder</span></code></a>,
  270. respectively. Incremental codecs can maintain state.</p>
  271. </dd></dl>
  272. <dl class="py attribute">
  273. <dt class="sig sig-object py" id="codecs.CodecInfo.streamwriter">
  274. <span class="sig-name descname"><span class="pre">streamwriter</span></span><a class="headerlink" href="#codecs.CodecInfo.streamwriter" title="Link to this definition">¶</a></dt>
  275. <dt class="sig sig-object py" id="codecs.CodecInfo.streamreader">
  276. <span class="sig-name descname"><span class="pre">streamreader</span></span><a class="headerlink" href="#codecs.CodecInfo.streamreader" title="Link to this definition">¶</a></dt>
  277. <dd><p>Stream writer and reader classes or factory functions. These have to
  278. provide the interface defined by the base classes
  279. <a class="reference internal" href="#codecs.StreamWriter" title="codecs.StreamWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamWriter</span></code></a> and <a class="reference internal" href="#codecs.StreamReader" title="codecs.StreamReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code></a>, respectively.
  280. Stream codecs can maintain state.</p>
  281. </dd></dl>
  282. </dd></dl>
  283. <p>To simplify access to the various codec components, the module provides
  284. these additional functions which use <a class="reference internal" href="#codecs.lookup" title="codecs.lookup"><code class="xref py py-func docutils literal notranslate"><span class="pre">lookup()</span></code></a> for the codec lookup:</p>
  285. <dl class="py function">
  286. <dt class="sig sig-object py" id="codecs.getencoder">
  287. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">getencoder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.getencoder" title="Link to this definition">¶</a></dt>
  288. <dd><p>Look up the codec for the given encoding and return its encoder function.</p>
  289. <p>Raises a <a class="reference internal" href="exceptions.html#LookupError" title="LookupError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">LookupError</span></code></a> in case the encoding cannot be found.</p>
  290. </dd></dl>
  291. <dl class="py function">
  292. <dt class="sig sig-object py" id="codecs.getdecoder">
  293. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">getdecoder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.getdecoder" title="Link to this definition">¶</a></dt>
  294. <dd><p>Look up the codec for the given encoding and return its decoder function.</p>
  295. <p>Raises a <a class="reference internal" href="exceptions.html#LookupError" title="LookupError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">LookupError</span></code></a> in case the encoding cannot be found.</p>
  296. </dd></dl>
  297. <dl class="py function">
  298. <dt class="sig sig-object py" id="codecs.getincrementalencoder">
  299. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">getincrementalencoder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.getincrementalencoder" title="Link to this definition">¶</a></dt>
  300. <dd><p>Look up the codec for the given encoding and return its incremental encoder
  301. class or factory function.</p>
  302. <p>Raises a <a class="reference internal" href="exceptions.html#LookupError" title="LookupError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">LookupError</span></code></a> in case the encoding cannot be found or the codec
  303. doesn’t support an incremental encoder.</p>
  304. </dd></dl>
  305. <dl class="py function">
  306. <dt class="sig sig-object py" id="codecs.getincrementaldecoder">
  307. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">getincrementaldecoder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.getincrementaldecoder" title="Link to this definition">¶</a></dt>
  308. <dd><p>Look up the codec for the given encoding and return its incremental decoder
  309. class or factory function.</p>
  310. <p>Raises a <a class="reference internal" href="exceptions.html#LookupError" title="LookupError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">LookupError</span></code></a> in case the encoding cannot be found or the codec
  311. doesn’t support an incremental decoder.</p>
  312. </dd></dl>
  313. <dl class="py function">
  314. <dt class="sig sig-object py" id="codecs.getreader">
  315. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">getreader</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.getreader" title="Link to this definition">¶</a></dt>
  316. <dd><p>Look up the codec for the given encoding and return its <a class="reference internal" href="#codecs.StreamReader" title="codecs.StreamReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code></a>
  317. class or factory function.</p>
  318. <p>Raises a <a class="reference internal" href="exceptions.html#LookupError" title="LookupError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">LookupError</span></code></a> in case the encoding cannot be found.</p>
  319. </dd></dl>
  320. <dl class="py function">
  321. <dt class="sig sig-object py" id="codecs.getwriter">
  322. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">getwriter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.getwriter" title="Link to this definition">¶</a></dt>
  323. <dd><p>Look up the codec for the given encoding and return its <a class="reference internal" href="#codecs.StreamWriter" title="codecs.StreamWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamWriter</span></code></a>
  324. class or factory function.</p>
  325. <p>Raises a <a class="reference internal" href="exceptions.html#LookupError" title="LookupError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">LookupError</span></code></a> in case the encoding cannot be found.</p>
  326. </dd></dl>
  327. <p>Custom codecs are made available by registering a suitable codec search
  328. function:</p>
  329. <dl class="py function">
  330. <dt class="sig sig-object py" id="codecs.register">
  331. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">register</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">search_function</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.register" title="Link to this definition">¶</a></dt>
  332. <dd><p>Register a codec search function. Search functions are expected to take one
  333. argument, being the encoding name in all lower case letters with hyphens
  334. and spaces converted to underscores, and return a <a class="reference internal" href="#codecs.CodecInfo" title="codecs.CodecInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">CodecInfo</span></code></a> object.
  335. In case a search function cannot find a given encoding, it should return
  336. <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  337. <div class="versionchanged">
  338. <p><span class="versionmodified changed">Changed in version 3.9: </span>Hyphens and spaces are converted to underscore.</p>
  339. </div>
  340. </dd></dl>
  341. <dl class="py function">
  342. <dt class="sig sig-object py" id="codecs.unregister">
  343. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">unregister</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">search_function</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.unregister" title="Link to this definition">¶</a></dt>
  344. <dd><p>Unregister a codec search function and clear the registry’s cache.
  345. If the search function is not registered, do nothing.</p>
  346. <div class="versionadded">
  347. <p><span class="versionmodified added">New in version 3.10.</span></p>
  348. </div>
  349. </dd></dl>
  350. <p>While the builtin <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> and the associated <a class="reference internal" href="io.html#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code></a> module are the
  351. recommended approach for working with encoded text files, this module
  352. provides additional utility functions and classes that allow the use of a
  353. wider range of codecs when working with binary files:</p>
  354. <dl class="py function">
  355. <dt class="sig sig-object py" id="codecs.open">
  356. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'r'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'strict'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffering</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.open" title="Link to this definition">¶</a></dt>
  357. <dd><p>Open an encoded file using the given <em>mode</em> and return an instance of
  358. <a class="reference internal" href="#codecs.StreamReaderWriter" title="codecs.StreamReaderWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReaderWriter</span></code></a>, providing transparent encoding/decoding.
  359. The default file mode is <code class="docutils literal notranslate"><span class="pre">'r'</span></code>, meaning to open the file in read mode.</p>
  360. <div class="admonition note">
  361. <p class="admonition-title">Note</p>
  362. <p>If <em>encoding</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, then the
  363. underlying encoded files are always opened in binary mode.
  364. No automatic conversion of <code class="docutils literal notranslate"><span class="pre">'\n'</span></code> is done on reading and writing.
  365. The <em>mode</em> argument may be any binary mode acceptable to the built-in
  366. <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> function; the <code class="docutils literal notranslate"><span class="pre">'b'</span></code> is automatically added.</p>
  367. </div>
  368. <p><em>encoding</em> specifies the encoding which is to be used for the file.
  369. Any encoding that encodes to and decodes from bytes is allowed, and
  370. the data types supported by the file methods depend on the codec used.</p>
  371. <p><em>errors</em> may be given to define the error handling. It defaults to <code class="docutils literal notranslate"><span class="pre">'strict'</span></code>
  372. which causes a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> to be raised in case an encoding error occurs.</p>
  373. <p><em>buffering</em> has the same meaning as for the built-in <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> function.
  374. It defaults to -1 which means that the default buffer size will be used.</p>
  375. <div class="versionchanged">
  376. <p><span class="versionmodified changed">Changed in version 3.11: </span>The <code class="docutils literal notranslate"><span class="pre">'U'</span></code> mode has been removed.</p>
  377. </div>
  378. </dd></dl>
  379. <dl class="py function">
  380. <dt class="sig sig-object py" id="codecs.EncodedFile">
  381. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">EncodedFile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">file</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">data_encoding</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">file_encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'strict'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.EncodedFile" title="Link to this definition">¶</a></dt>
  382. <dd><p>Return a <a class="reference internal" href="#codecs.StreamRecoder" title="codecs.StreamRecoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamRecoder</span></code></a> instance, a wrapped version of <em>file</em>
  383. which provides transparent transcoding. The original file is closed
  384. when the wrapped version is closed.</p>
  385. <p>Data written to the wrapped file is decoded according to the given
  386. <em>data_encoding</em> and then written to the original file as bytes using
  387. <em>file_encoding</em>. Bytes read from the original file are decoded
  388. according to <em>file_encoding</em>, and the result is encoded
  389. using <em>data_encoding</em>.</p>
  390. <p>If <em>file_encoding</em> is not given, it defaults to <em>data_encoding</em>.</p>
  391. <p><em>errors</em> may be given to define the error handling. It defaults to
  392. <code class="docutils literal notranslate"><span class="pre">'strict'</span></code>, which causes <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> to be raised in case an encoding
  393. error occurs.</p>
  394. </dd></dl>
  395. <dl class="py function">
  396. <dt class="sig sig-object py" id="codecs.iterencode">
  397. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">iterencode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterator</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'strict'</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.iterencode" title="Link to this definition">¶</a></dt>
  398. <dd><p>Uses an incremental encoder to iteratively encode the input provided by
  399. <em>iterator</em>. This function is a <a class="reference internal" href="../glossary.html#term-generator"><span class="xref std std-term">generator</span></a>.
  400. The <em>errors</em> argument (as well as any
  401. other keyword argument) is passed through to the incremental encoder.</p>
  402. <p>This function requires that the codec accept text <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> objects
  403. to encode. Therefore it does not support bytes-to-bytes encoders such as
  404. <code class="docutils literal notranslate"><span class="pre">base64_codec</span></code>.</p>
  405. </dd></dl>
  406. <dl class="py function">
  407. <dt class="sig sig-object py" id="codecs.iterdecode">
  408. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">iterdecode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterator</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'strict'</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.iterdecode" title="Link to this definition">¶</a></dt>
  409. <dd><p>Uses an incremental decoder to iteratively decode the input provided by
  410. <em>iterator</em>. This function is a <a class="reference internal" href="../glossary.html#term-generator"><span class="xref std std-term">generator</span></a>.
  411. The <em>errors</em> argument (as well as any
  412. other keyword argument) is passed through to the incremental decoder.</p>
  413. <p>This function requires that the codec accept <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> objects
  414. to decode. Therefore it does not support text-to-text encoders such as
  415. <code class="docutils literal notranslate"><span class="pre">rot_13</span></code>, although <code class="docutils literal notranslate"><span class="pre">rot_13</span></code> may be used equivalently with
  416. <a class="reference internal" href="#codecs.iterencode" title="codecs.iterencode"><code class="xref py py-func docutils literal notranslate"><span class="pre">iterencode()</span></code></a>.</p>
  417. </dd></dl>
  418. <p>The module also provides the following constants which are useful for reading
  419. and writing to platform dependent files:</p>
  420. <dl class="py data">
  421. <dt class="sig sig-object py" id="codecs.BOM">
  422. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">BOM</span></span><a class="headerlink" href="#codecs.BOM" title="Link to this definition">¶</a></dt>
  423. <dt class="sig sig-object py" id="codecs.BOM_BE">
  424. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">BOM_BE</span></span><a class="headerlink" href="#codecs.BOM_BE" title="Link to this definition">¶</a></dt>
  425. <dt class="sig sig-object py" id="codecs.BOM_LE">
  426. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">BOM_LE</span></span><a class="headerlink" href="#codecs.BOM_LE" title="Link to this definition">¶</a></dt>
  427. <dt class="sig sig-object py" id="codecs.BOM_UTF8">
  428. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">BOM_UTF8</span></span><a class="headerlink" href="#codecs.BOM_UTF8" title="Link to this definition">¶</a></dt>
  429. <dt class="sig sig-object py" id="codecs.BOM_UTF16">
  430. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">BOM_UTF16</span></span><a class="headerlink" href="#codecs.BOM_UTF16" title="Link to this definition">¶</a></dt>
  431. <dt class="sig sig-object py" id="codecs.BOM_UTF16_BE">
  432. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">BOM_UTF16_BE</span></span><a class="headerlink" href="#codecs.BOM_UTF16_BE" title="Link to this definition">¶</a></dt>
  433. <dt class="sig sig-object py" id="codecs.BOM_UTF16_LE">
  434. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">BOM_UTF16_LE</span></span><a class="headerlink" href="#codecs.BOM_UTF16_LE" title="Link to this definition">¶</a></dt>
  435. <dt class="sig sig-object py" id="codecs.BOM_UTF32">
  436. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">BOM_UTF32</span></span><a class="headerlink" href="#codecs.BOM_UTF32" title="Link to this definition">¶</a></dt>
  437. <dt class="sig sig-object py" id="codecs.BOM_UTF32_BE">
  438. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">BOM_UTF32_BE</span></span><a class="headerlink" href="#codecs.BOM_UTF32_BE" title="Link to this definition">¶</a></dt>
  439. <dt class="sig sig-object py" id="codecs.BOM_UTF32_LE">
  440. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">BOM_UTF32_LE</span></span><a class="headerlink" href="#codecs.BOM_UTF32_LE" title="Link to this definition">¶</a></dt>
  441. <dd><p>These constants define various byte sequences,
  442. being Unicode byte order marks (BOMs) for several encodings. They are
  443. used in UTF-16 and UTF-32 data streams to indicate the byte order used,
  444. and in UTF-8 as a Unicode signature. <a class="reference internal" href="#codecs.BOM_UTF16" title="codecs.BOM_UTF16"><code class="xref py py-const docutils literal notranslate"><span class="pre">BOM_UTF16</span></code></a> is either
  445. <a class="reference internal" href="#codecs.BOM_UTF16_BE" title="codecs.BOM_UTF16_BE"><code class="xref py py-const docutils literal notranslate"><span class="pre">BOM_UTF16_BE</span></code></a> or <a class="reference internal" href="#codecs.BOM_UTF16_LE" title="codecs.BOM_UTF16_LE"><code class="xref py py-const docutils literal notranslate"><span class="pre">BOM_UTF16_LE</span></code></a> depending on the platform’s
  446. native byte order, <a class="reference internal" href="#codecs.BOM" title="codecs.BOM"><code class="xref py py-const docutils literal notranslate"><span class="pre">BOM</span></code></a> is an alias for <a class="reference internal" href="#codecs.BOM_UTF16" title="codecs.BOM_UTF16"><code class="xref py py-const docutils literal notranslate"><span class="pre">BOM_UTF16</span></code></a>,
  447. <a class="reference internal" href="#codecs.BOM_LE" title="codecs.BOM_LE"><code class="xref py py-const docutils literal notranslate"><span class="pre">BOM_LE</span></code></a> for <a class="reference internal" href="#codecs.BOM_UTF16_LE" title="codecs.BOM_UTF16_LE"><code class="xref py py-const docutils literal notranslate"><span class="pre">BOM_UTF16_LE</span></code></a> and <a class="reference internal" href="#codecs.BOM_BE" title="codecs.BOM_BE"><code class="xref py py-const docutils literal notranslate"><span class="pre">BOM_BE</span></code></a> for
  448. <a class="reference internal" href="#codecs.BOM_UTF16_BE" title="codecs.BOM_UTF16_BE"><code class="xref py py-const docutils literal notranslate"><span class="pre">BOM_UTF16_BE</span></code></a>. The others represent the BOM in UTF-8 and UTF-32
  449. encodings.</p>
  450. </dd></dl>
  451. <section id="codec-base-classes">
  452. <span id="id1"></span><h2>Codec Base Classes<a class="headerlink" href="#codec-base-classes" title="Link to this heading">¶</a></h2>
  453. <p>The <a class="reference internal" href="#module-codecs" title="codecs: Encode and decode data and streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">codecs</span></code></a> module defines a set of base classes which define the
  454. interfaces for working with codec objects, and can also be used as the basis
  455. for custom codec implementations.</p>
  456. <p>Each codec has to define four interfaces to make it usable as codec in Python:
  457. stateless encoder, stateless decoder, stream reader and stream writer. The
  458. stream reader and writers typically reuse the stateless encoder/decoder to
  459. implement the file protocols. Codec authors also need to define how the
  460. codec will handle encoding and decoding errors.</p>
  461. <section id="error-handlers">
  462. <span id="surrogateescape"></span><span id="id2"></span><h3>Error Handlers<a class="headerlink" href="#error-handlers" title="Link to this heading">¶</a></h3>
  463. <p>To simplify and standardize error handling, codecs may implement different
  464. error handling schemes by accepting the <em>errors</em> string argument:</p>
  465. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;German ß, ♬&#39;</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="n">encoding</span><span class="o">=</span><span class="s1">&#39;ascii&#39;</span><span class="p">,</span> <span class="n">errors</span><span class="o">=</span><span class="s1">&#39;backslashreplace&#39;</span><span class="p">)</span>
  466. <span class="go">b&#39;German \\xdf, \\u266c&#39;</span>
  467. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;German ß, ♬&#39;</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="n">encoding</span><span class="o">=</span><span class="s1">&#39;ascii&#39;</span><span class="p">,</span> <span class="n">errors</span><span class="o">=</span><span class="s1">&#39;xmlcharrefreplace&#39;</span><span class="p">)</span>
  468. <span class="go">b&#39;German &amp;#223;, &amp;#9836;&#39;</span>
  469. </pre></div>
  470. </div>
  471. <p id="index-1">The following error handlers can be used with all Python
  472. <a class="reference internal" href="#standard-encodings"><span class="std std-ref">Standard Encodings</span></a> codecs:</p>
  473. <table class="docutils align-default">
  474. <thead>
  475. <tr class="row-odd"><th class="head"><p>Value</p></th>
  476. <th class="head"><p>Meaning</p></th>
  477. </tr>
  478. </thead>
  479. <tbody>
  480. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'strict'</span></code></p></td>
  481. <td><p>Raise <a class="reference internal" href="exceptions.html#UnicodeError" title="UnicodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeError</span></code></a> (or a subclass),
  482. this is the default. Implemented in
  483. <a class="reference internal" href="#codecs.strict_errors" title="codecs.strict_errors"><code class="xref py py-func docutils literal notranslate"><span class="pre">strict_errors()</span></code></a>.</p></td>
  484. </tr>
  485. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'ignore'</span></code></p></td>
  486. <td><p>Ignore the malformed data and continue without
  487. further notice. Implemented in
  488. <a class="reference internal" href="#codecs.ignore_errors" title="codecs.ignore_errors"><code class="xref py py-func docutils literal notranslate"><span class="pre">ignore_errors()</span></code></a>.</p></td>
  489. </tr>
  490. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'replace'</span></code></p></td>
  491. <td><p>Replace with a replacement marker. On
  492. encoding, use <code class="docutils literal notranslate"><span class="pre">?</span></code> (ASCII character). On
  493. decoding, use <code class="docutils literal notranslate"><span class="pre">�</span></code> (U+FFFD, the official
  494. REPLACEMENT CHARACTER). Implemented in
  495. <a class="reference internal" href="#codecs.replace_errors" title="codecs.replace_errors"><code class="xref py py-func docutils literal notranslate"><span class="pre">replace_errors()</span></code></a>.</p></td>
  496. </tr>
  497. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'backslashreplace'</span></code></p></td>
  498. <td><p>Replace with backslashed escape sequences.
  499. On encoding, use hexadecimal form of Unicode
  500. code point with formats <code class="samp docutils literal notranslate"><span class="pre">\x</span><em><span class="pre">hh</span></em></code>
  501. <code class="samp docutils literal notranslate"><span class="pre">\u</span><em><span class="pre">xxxx</span></em></code> <code class="samp docutils literal notranslate"><span class="pre">\U</span><em><span class="pre">xxxxxxxx</span></em></code>.
  502. On decoding, use hexadecimal form of byte
  503. value with format <code class="samp docutils literal notranslate"><span class="pre">\x</span><em><span class="pre">hh</span></em></code>.
  504. Implemented in
  505. <a class="reference internal" href="#codecs.backslashreplace_errors" title="codecs.backslashreplace_errors"><code class="xref py py-func docutils literal notranslate"><span class="pre">backslashreplace_errors()</span></code></a>.</p></td>
  506. </tr>
  507. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'surrogateescape'</span></code></p></td>
  508. <td><p>On decoding, replace byte with individual
  509. surrogate code ranging from <code class="docutils literal notranslate"><span class="pre">U+DC80</span></code> to
  510. <code class="docutils literal notranslate"><span class="pre">U+DCFF</span></code>. This code will then be turned
  511. back into the same byte when the
  512. <code class="docutils literal notranslate"><span class="pre">'surrogateescape'</span></code> error handler is used
  513. when encoding the data. (See <span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0383/"><strong>PEP 383</strong></a> for
  514. more.)</p></td>
  515. </tr>
  516. </tbody>
  517. </table>
  518. <p id="index-3">The following error handlers are only applicable to encoding (within
  519. <a class="reference internal" href="../glossary.html#term-text-encoding"><span class="xref std std-term">text encodings</span></a>):</p>
  520. <table class="docutils align-default">
  521. <thead>
  522. <tr class="row-odd"><th class="head"><p>Value</p></th>
  523. <th class="head"><p>Meaning</p></th>
  524. </tr>
  525. </thead>
  526. <tbody>
  527. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'xmlcharrefreplace'</span></code></p></td>
  528. <td><p>Replace with XML/HTML numeric character
  529. reference, which is a decimal form of Unicode
  530. code point with format <code class="samp docutils literal notranslate"><span class="pre">&amp;#</span><em><span class="pre">num</span></em><span class="pre">;</span></code>.
  531. Implemented in
  532. <a class="reference internal" href="#codecs.xmlcharrefreplace_errors" title="codecs.xmlcharrefreplace_errors"><code class="xref py py-func docutils literal notranslate"><span class="pre">xmlcharrefreplace_errors()</span></code></a>.</p></td>
  533. </tr>
  534. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'namereplace'</span></code></p></td>
  535. <td><p>Replace with <code class="docutils literal notranslate"><span class="pre">\N{...}</span></code> escape sequences,
  536. what appears in the braces is the Name
  537. property from Unicode Character Database.
  538. Implemented in <a class="reference internal" href="#codecs.namereplace_errors" title="codecs.namereplace_errors"><code class="xref py py-func docutils literal notranslate"><span class="pre">namereplace_errors()</span></code></a>.</p></td>
  539. </tr>
  540. </tbody>
  541. </table>
  542. <p id="index-4">In addition, the following error handler is specific to the given codecs:</p>
  543. <table class="docutils align-default">
  544. <thead>
  545. <tr class="row-odd"><th class="head"><p>Value</p></th>
  546. <th class="head"><p>Codecs</p></th>
  547. <th class="head"><p>Meaning</p></th>
  548. </tr>
  549. </thead>
  550. <tbody>
  551. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'surrogatepass'</span></code></p></td>
  552. <td><p>utf-8, utf-16, utf-32,
  553. utf-16-be, utf-16-le,
  554. utf-32-be, utf-32-le</p></td>
  555. <td><p>Allow encoding and decoding surrogate code
  556. point (<code class="docutils literal notranslate"><span class="pre">U+D800</span></code> - <code class="docutils literal notranslate"><span class="pre">U+DFFF</span></code>) as normal
  557. code point. Otherwise these codecs treat
  558. the presence of surrogate code point in
  559. <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> as an error.</p></td>
  560. </tr>
  561. </tbody>
  562. </table>
  563. <div class="versionadded">
  564. <p><span class="versionmodified added">New in version 3.1: </span>The <code class="docutils literal notranslate"><span class="pre">'surrogateescape'</span></code> and <code class="docutils literal notranslate"><span class="pre">'surrogatepass'</span></code> error handlers.</p>
  565. </div>
  566. <div class="versionchanged">
  567. <p><span class="versionmodified changed">Changed in version 3.4: </span>The <code class="docutils literal notranslate"><span class="pre">'surrogatepass'</span></code> error handler now works with utf-16* and utf-32*
  568. codecs.</p>
  569. </div>
  570. <div class="versionadded">
  571. <p><span class="versionmodified added">New in version 3.5: </span>The <code class="docutils literal notranslate"><span class="pre">'namereplace'</span></code> error handler.</p>
  572. </div>
  573. <div class="versionchanged">
  574. <p><span class="versionmodified changed">Changed in version 3.5: </span>The <code class="docutils literal notranslate"><span class="pre">'backslashreplace'</span></code> error handler now works with decoding and
  575. translating.</p>
  576. </div>
  577. <p>The set of allowed values can be extended by registering a new named error
  578. handler:</p>
  579. <dl class="py function">
  580. <dt class="sig sig-object py" id="codecs.register_error">
  581. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">register_error</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">error_handler</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.register_error" title="Link to this definition">¶</a></dt>
  582. <dd><p>Register the error handling function <em>error_handler</em> under the name <em>name</em>.
  583. The <em>error_handler</em> argument will be called during encoding and decoding
  584. in case of an error, when <em>name</em> is specified as the errors parameter.</p>
  585. <p>For encoding, <em>error_handler</em> will be called with a <a class="reference internal" href="exceptions.html#UnicodeEncodeError" title="UnicodeEncodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeEncodeError</span></code></a>
  586. instance, which contains information about the location of the error. The
  587. error handler must either raise this or a different exception, or return a
  588. tuple with a replacement for the unencodable part of the input and a position
  589. where encoding should continue. The replacement may be either <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> or
  590. <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>. If the replacement is bytes, the encoder will simply copy
  591. them into the output buffer. If the replacement is a string, the encoder will
  592. encode the replacement. Encoding continues on original input at the
  593. specified position. Negative position values will be treated as being
  594. relative to the end of the input string. If the resulting position is out of
  595. bound an <a class="reference internal" href="exceptions.html#IndexError" title="IndexError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndexError</span></code></a> will be raised.</p>
  596. <p>Decoding and translating works similarly, except <a class="reference internal" href="exceptions.html#UnicodeDecodeError" title="UnicodeDecodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeDecodeError</span></code></a> or
  597. <a class="reference internal" href="exceptions.html#UnicodeTranslateError" title="UnicodeTranslateError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeTranslateError</span></code></a> will be passed to the handler and that the
  598. replacement from the error handler will be put into the output directly.</p>
  599. </dd></dl>
  600. <p>Previously registered error handlers (including the standard error handlers)
  601. can be looked up by name:</p>
  602. <dl class="py function">
  603. <dt class="sig sig-object py" id="codecs.lookup_error">
  604. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">lookup_error</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.lookup_error" title="Link to this definition">¶</a></dt>
  605. <dd><p>Return the error handler previously registered under the name <em>name</em>.</p>
  606. <p>Raises a <a class="reference internal" href="exceptions.html#LookupError" title="LookupError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">LookupError</span></code></a> in case the handler cannot be found.</p>
  607. </dd></dl>
  608. <p>The following standard error handlers are also made available as module level
  609. functions:</p>
  610. <dl class="py function">
  611. <dt class="sig sig-object py" id="codecs.strict_errors">
  612. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">strict_errors</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exception</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.strict_errors" title="Link to this definition">¶</a></dt>
  613. <dd><p>Implements the <code class="docutils literal notranslate"><span class="pre">'strict'</span></code> error handling.</p>
  614. <p>Each encoding or decoding error raises a <a class="reference internal" href="exceptions.html#UnicodeError" title="UnicodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeError</span></code></a>.</p>
  615. </dd></dl>
  616. <dl class="py function">
  617. <dt class="sig sig-object py" id="codecs.ignore_errors">
  618. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">ignore_errors</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exception</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.ignore_errors" title="Link to this definition">¶</a></dt>
  619. <dd><p>Implements the <code class="docutils literal notranslate"><span class="pre">'ignore'</span></code> error handling.</p>
  620. <p>Malformed data is ignored; encoding or decoding is continued without
  621. further notice.</p>
  622. </dd></dl>
  623. <dl class="py function">
  624. <dt class="sig sig-object py" id="codecs.replace_errors">
  625. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">replace_errors</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exception</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.replace_errors" title="Link to this definition">¶</a></dt>
  626. <dd><p>Implements the <code class="docutils literal notranslate"><span class="pre">'replace'</span></code> error handling.</p>
  627. <p>Substitutes <code class="docutils literal notranslate"><span class="pre">?</span></code> (ASCII character) for encoding errors or <code class="docutils literal notranslate"><span class="pre">�</span></code> (U+FFFD,
  628. the official REPLACEMENT CHARACTER) for decoding errors.</p>
  629. </dd></dl>
  630. <dl class="py function">
  631. <dt class="sig sig-object py" id="codecs.backslashreplace_errors">
  632. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">backslashreplace_errors</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exception</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.backslashreplace_errors" title="Link to this definition">¶</a></dt>
  633. <dd><p>Implements the <code class="docutils literal notranslate"><span class="pre">'backslashreplace'</span></code> error handling.</p>
  634. <p>Malformed data is replaced by a backslashed escape sequence.
  635. On encoding, use the hexadecimal form of Unicode code point with formats
  636. <code class="samp docutils literal notranslate"><span class="pre">\x</span><em><span class="pre">hh</span></em></code> <code class="samp docutils literal notranslate"><span class="pre">\u</span><em><span class="pre">xxxx</span></em></code> <code class="samp docutils literal notranslate"><span class="pre">\U</span><em><span class="pre">xxxxxxxx</span></em></code>.
  637. On decoding, use the hexadecimal form of
  638. byte value with format <code class="samp docutils literal notranslate"><span class="pre">\x</span><em><span class="pre">hh</span></em></code>.</p>
  639. <div class="versionchanged">
  640. <p><span class="versionmodified changed">Changed in version 3.5: </span>Works with decoding and translating.</p>
  641. </div>
  642. </dd></dl>
  643. <dl class="py function">
  644. <dt class="sig sig-object py" id="codecs.xmlcharrefreplace_errors">
  645. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">xmlcharrefreplace_errors</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exception</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.xmlcharrefreplace_errors" title="Link to this definition">¶</a></dt>
  646. <dd><p>Implements the <code class="docutils literal notranslate"><span class="pre">'xmlcharrefreplace'</span></code> error handling (for encoding within
  647. <a class="reference internal" href="../glossary.html#term-text-encoding"><span class="xref std std-term">text encoding</span></a> only).</p>
  648. <p>The unencodable character is replaced by an appropriate XML/HTML numeric
  649. character reference, which is a decimal form of Unicode code point with
  650. format <code class="samp docutils literal notranslate"><span class="pre">&amp;#</span><em><span class="pre">num</span></em><span class="pre">;</span></code> .</p>
  651. </dd></dl>
  652. <dl class="py function">
  653. <dt class="sig sig-object py" id="codecs.namereplace_errors">
  654. <span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">namereplace_errors</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exception</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.namereplace_errors" title="Link to this definition">¶</a></dt>
  655. <dd><p>Implements the <code class="docutils literal notranslate"><span class="pre">'namereplace'</span></code> error handling (for encoding within
  656. <a class="reference internal" href="../glossary.html#term-text-encoding"><span class="xref std std-term">text encoding</span></a> only).</p>
  657. <p>The unencodable character is replaced by a <code class="docutils literal notranslate"><span class="pre">\N{...}</span></code> escape sequence. The
  658. set of characters that appear in the braces is the Name property from
  659. Unicode Character Database. For example, the German lowercase letter <code class="docutils literal notranslate"><span class="pre">'ß'</span></code>
  660. will be converted to byte sequence <code class="docutils literal notranslate"><span class="pre">\N{LATIN</span> <span class="pre">SMALL</span> <span class="pre">LETTER</span> <span class="pre">SHARP</span> <span class="pre">S}</span></code> .</p>
  661. <div class="versionadded">
  662. <p><span class="versionmodified added">New in version 3.5.</span></p>
  663. </div>
  664. </dd></dl>
  665. </section>
  666. <section id="stateless-encoding-and-decoding">
  667. <span id="codec-objects"></span><h3>Stateless Encoding and Decoding<a class="headerlink" href="#stateless-encoding-and-decoding" title="Link to this heading">¶</a></h3>
  668. <p>The base <a class="reference internal" href="#codecs.Codec" title="codecs.Codec"><code class="xref py py-class docutils literal notranslate"><span class="pre">Codec</span></code></a> class defines these methods which also define the
  669. function interfaces of the stateless encoder and decoder:</p>
  670. <dl class="py class">
  671. <dt class="sig sig-object py" id="codecs.Codec">
  672. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">Codec</span></span><a class="headerlink" href="#codecs.Codec" title="Link to this definition">¶</a></dt>
  673. <dd><dl class="py method">
  674. <dt class="sig sig-object py" id="codecs.Codec.encode">
  675. <span class="sig-name descname"><span class="pre">encode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">input</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'strict'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.Codec.encode" title="Link to this definition">¶</a></dt>
  676. <dd><p>Encodes the object <em>input</em> and returns a tuple (output object, length consumed).
  677. For instance, <a class="reference internal" href="../glossary.html#term-text-encoding"><span class="xref std std-term">text encoding</span></a> converts
  678. a string object to a bytes object using a particular
  679. character set encoding (e.g., <code class="docutils literal notranslate"><span class="pre">cp1252</span></code> or <code class="docutils literal notranslate"><span class="pre">iso-8859-1</span></code>).</p>
  680. <p>The <em>errors</em> argument defines the error handling to apply.
  681. It defaults to <code class="docutils literal notranslate"><span class="pre">'strict'</span></code> handling.</p>
  682. <p>The method may not store state in the <a class="reference internal" href="#codecs.Codec" title="codecs.Codec"><code class="xref py py-class docutils literal notranslate"><span class="pre">Codec</span></code></a> instance. Use
  683. <a class="reference internal" href="#codecs.StreamWriter" title="codecs.StreamWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamWriter</span></code></a> for codecs which have to keep state in order to make
  684. encoding efficient.</p>
  685. <p>The encoder must be able to handle zero length input and return an empty object
  686. of the output object type in this situation.</p>
  687. </dd></dl>
  688. <dl class="py method">
  689. <dt class="sig sig-object py" id="codecs.Codec.decode">
  690. <span class="sig-name descname"><span class="pre">decode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">input</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'strict'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.Codec.decode" title="Link to this definition">¶</a></dt>
  691. <dd><p>Decodes the object <em>input</em> and returns a tuple (output object, length
  692. consumed). For instance, for a <a class="reference internal" href="../glossary.html#term-text-encoding"><span class="xref std std-term">text encoding</span></a>, decoding converts
  693. a bytes object encoded using a particular
  694. character set encoding to a string object.</p>
  695. <p>For text encodings and bytes-to-bytes codecs,
  696. <em>input</em> must be a bytes object or one which provides the read-only
  697. buffer interface – for example, buffer objects and memory mapped files.</p>
  698. <p>The <em>errors</em> argument defines the error handling to apply.
  699. It defaults to <code class="docutils literal notranslate"><span class="pre">'strict'</span></code> handling.</p>
  700. <p>The method may not store state in the <a class="reference internal" href="#codecs.Codec" title="codecs.Codec"><code class="xref py py-class docutils literal notranslate"><span class="pre">Codec</span></code></a> instance. Use
  701. <a class="reference internal" href="#codecs.StreamReader" title="codecs.StreamReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code></a> for codecs which have to keep state in order to make
  702. decoding efficient.</p>
  703. <p>The decoder must be able to handle zero length input and return an empty object
  704. of the output object type in this situation.</p>
  705. </dd></dl>
  706. </dd></dl>
  707. </section>
  708. <section id="incremental-encoding-and-decoding">
  709. <h3>Incremental Encoding and Decoding<a class="headerlink" href="#incremental-encoding-and-decoding" title="Link to this heading">¶</a></h3>
  710. <p>The <a class="reference internal" href="#codecs.IncrementalEncoder" title="codecs.IncrementalEncoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">IncrementalEncoder</span></code></a> and <a class="reference internal" href="#codecs.IncrementalDecoder" title="codecs.IncrementalDecoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">IncrementalDecoder</span></code></a> classes provide
  711. the basic interface for incremental encoding and decoding. Encoding/decoding the
  712. input isn’t done with one call to the stateless encoder/decoder function, but
  713. with multiple calls to the
  714. <a class="reference internal" href="#codecs.IncrementalEncoder.encode" title="codecs.IncrementalEncoder.encode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">encode()</span></code></a>/<a class="reference internal" href="#codecs.IncrementalDecoder.decode" title="codecs.IncrementalDecoder.decode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decode()</span></code></a> method of
  715. the incremental encoder/decoder. The incremental encoder/decoder keeps track of
  716. the encoding/decoding process during method calls.</p>
  717. <p>The joined output of calls to the
  718. <a class="reference internal" href="#codecs.IncrementalEncoder.encode" title="codecs.IncrementalEncoder.encode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">encode()</span></code></a>/<a class="reference internal" href="#codecs.IncrementalDecoder.decode" title="codecs.IncrementalDecoder.decode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decode()</span></code></a> method is
  719. the same as if all the single inputs were joined into one, and this input was
  720. encoded/decoded with the stateless encoder/decoder.</p>
  721. <section id="incrementalencoder-objects">
  722. <span id="incremental-encoder-objects"></span><h4>IncrementalEncoder Objects<a class="headerlink" href="#incrementalencoder-objects" title="Link to this heading">¶</a></h4>
  723. <p>The <a class="reference internal" href="#codecs.IncrementalEncoder" title="codecs.IncrementalEncoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">IncrementalEncoder</span></code></a> class is used for encoding an input in multiple
  724. steps. It defines the following methods which every incremental encoder must
  725. define in order to be compatible with the Python codec registry.</p>
  726. <dl class="py class">
  727. <dt class="sig sig-object py" id="codecs.IncrementalEncoder">
  728. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">IncrementalEncoder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'strict'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.IncrementalEncoder" title="Link to this definition">¶</a></dt>
  729. <dd><p>Constructor for an <a class="reference internal" href="#codecs.IncrementalEncoder" title="codecs.IncrementalEncoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">IncrementalEncoder</span></code></a> instance.</p>
  730. <p>All incremental encoders must provide this constructor interface. They are free
  731. to add additional keyword arguments, but only the ones defined here are used by
  732. the Python codec registry.</p>
  733. <p>The <a class="reference internal" href="#codecs.IncrementalEncoder" title="codecs.IncrementalEncoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">IncrementalEncoder</span></code></a> may implement different error handling schemes
  734. by providing the <em>errors</em> keyword argument. See <a class="reference internal" href="#error-handlers"><span class="std std-ref">Error Handlers</span></a> for
  735. possible values.</p>
  736. <p>The <em>errors</em> argument will be assigned to an attribute of the same name.
  737. Assigning to this attribute makes it possible to switch between different error
  738. handling strategies during the lifetime of the <a class="reference internal" href="#codecs.IncrementalEncoder" title="codecs.IncrementalEncoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">IncrementalEncoder</span></code></a>
  739. object.</p>
  740. <dl class="py method">
  741. <dt class="sig sig-object py" id="codecs.IncrementalEncoder.encode">
  742. <span class="sig-name descname"><span class="pre">encode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">final</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.IncrementalEncoder.encode" title="Link to this definition">¶</a></dt>
  743. <dd><p>Encodes <em>object</em> (taking the current state of the encoder into account)
  744. and returns the resulting encoded object. If this is the last call to
  745. <a class="reference internal" href="#codecs.encode" title="codecs.encode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">encode()</span></code></a> <em>final</em> must be true (the default is false).</p>
  746. </dd></dl>
  747. <dl class="py method">
  748. <dt class="sig sig-object py" id="codecs.IncrementalEncoder.reset">
  749. <span class="sig-name descname"><span class="pre">reset</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#codecs.IncrementalEncoder.reset" title="Link to this definition">¶</a></dt>
  750. <dd><p>Reset the encoder to the initial state. The output is discarded: call
  751. <code class="docutils literal notranslate"><span class="pre">.encode(object,</span> <span class="pre">final=True)</span></code>, passing an empty byte or text string
  752. if necessary, to reset the encoder and to get the output.</p>
  753. </dd></dl>
  754. <dl class="py method">
  755. <dt class="sig sig-object py" id="codecs.IncrementalEncoder.getstate">
  756. <span class="sig-name descname"><span class="pre">getstate</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#codecs.IncrementalEncoder.getstate" title="Link to this definition">¶</a></dt>
  757. <dd><p>Return the current state of the encoder which must be an integer. The
  758. implementation should make sure that <code class="docutils literal notranslate"><span class="pre">0</span></code> is the most common
  759. state. (States that are more complicated than integers can be converted
  760. into an integer by marshaling/pickling the state and encoding the bytes
  761. of the resulting string into an integer.)</p>
  762. </dd></dl>
  763. <dl class="py method">
  764. <dt class="sig sig-object py" id="codecs.IncrementalEncoder.setstate">
  765. <span class="sig-name descname"><span class="pre">setstate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.IncrementalEncoder.setstate" title="Link to this definition">¶</a></dt>
  766. <dd><p>Set the state of the encoder to <em>state</em>. <em>state</em> must be an encoder state
  767. returned by <a class="reference internal" href="#codecs.IncrementalEncoder.getstate" title="codecs.IncrementalEncoder.getstate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getstate()</span></code></a>.</p>
  768. </dd></dl>
  769. </dd></dl>
  770. </section>
  771. <section id="incrementaldecoder-objects">
  772. <span id="incremental-decoder-objects"></span><h4>IncrementalDecoder Objects<a class="headerlink" href="#incrementaldecoder-objects" title="Link to this heading">¶</a></h4>
  773. <p>The <a class="reference internal" href="#codecs.IncrementalDecoder" title="codecs.IncrementalDecoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">IncrementalDecoder</span></code></a> class is used for decoding an input in multiple
  774. steps. It defines the following methods which every incremental decoder must
  775. define in order to be compatible with the Python codec registry.</p>
  776. <dl class="py class">
  777. <dt class="sig sig-object py" id="codecs.IncrementalDecoder">
  778. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">IncrementalDecoder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'strict'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.IncrementalDecoder" title="Link to this definition">¶</a></dt>
  779. <dd><p>Constructor for an <a class="reference internal" href="#codecs.IncrementalDecoder" title="codecs.IncrementalDecoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">IncrementalDecoder</span></code></a> instance.</p>
  780. <p>All incremental decoders must provide this constructor interface. They are free
  781. to add additional keyword arguments, but only the ones defined here are used by
  782. the Python codec registry.</p>
  783. <p>The <a class="reference internal" href="#codecs.IncrementalDecoder" title="codecs.IncrementalDecoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">IncrementalDecoder</span></code></a> may implement different error handling schemes
  784. by providing the <em>errors</em> keyword argument. See <a class="reference internal" href="#error-handlers"><span class="std std-ref">Error Handlers</span></a> for
  785. possible values.</p>
  786. <p>The <em>errors</em> argument will be assigned to an attribute of the same name.
  787. Assigning to this attribute makes it possible to switch between different error
  788. handling strategies during the lifetime of the <a class="reference internal" href="#codecs.IncrementalDecoder" title="codecs.IncrementalDecoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">IncrementalDecoder</span></code></a>
  789. object.</p>
  790. <dl class="py method">
  791. <dt class="sig sig-object py" id="codecs.IncrementalDecoder.decode">
  792. <span class="sig-name descname"><span class="pre">decode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">final</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.IncrementalDecoder.decode" title="Link to this definition">¶</a></dt>
  793. <dd><p>Decodes <em>object</em> (taking the current state of the decoder into account)
  794. and returns the resulting decoded object. If this is the last call to
  795. <a class="reference internal" href="#codecs.decode" title="codecs.decode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decode()</span></code></a> <em>final</em> must be true (the default is false). If <em>final</em> is
  796. true the decoder must decode the input completely and must flush all
  797. buffers. If this isn’t possible (e.g. because of incomplete byte sequences
  798. at the end of the input) it must initiate error handling just like in the
  799. stateless case (which might raise an exception).</p>
  800. </dd></dl>
  801. <dl class="py method">
  802. <dt class="sig sig-object py" id="codecs.IncrementalDecoder.reset">
  803. <span class="sig-name descname"><span class="pre">reset</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#codecs.IncrementalDecoder.reset" title="Link to this definition">¶</a></dt>
  804. <dd><p>Reset the decoder to the initial state.</p>
  805. </dd></dl>
  806. <dl class="py method">
  807. <dt class="sig sig-object py" id="codecs.IncrementalDecoder.getstate">
  808. <span class="sig-name descname"><span class="pre">getstate</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#codecs.IncrementalDecoder.getstate" title="Link to this definition">¶</a></dt>
  809. <dd><p>Return the current state of the decoder. This must be a tuple with two
  810. items, the first must be the buffer containing the still undecoded
  811. input. The second must be an integer and can be additional state
  812. info. (The implementation should make sure that <code class="docutils literal notranslate"><span class="pre">0</span></code> is the most common
  813. additional state info.) If this additional state info is <code class="docutils literal notranslate"><span class="pre">0</span></code> it must be
  814. possible to set the decoder to the state which has no input buffered and
  815. <code class="docutils literal notranslate"><span class="pre">0</span></code> as the additional state info, so that feeding the previously
  816. buffered input to the decoder returns it to the previous state without
  817. producing any output. (Additional state info that is more complicated than
  818. integers can be converted into an integer by marshaling/pickling the info
  819. and encoding the bytes of the resulting string into an integer.)</p>
  820. </dd></dl>
  821. <dl class="py method">
  822. <dt class="sig sig-object py" id="codecs.IncrementalDecoder.setstate">
  823. <span class="sig-name descname"><span class="pre">setstate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">state</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.IncrementalDecoder.setstate" title="Link to this definition">¶</a></dt>
  824. <dd><p>Set the state of the decoder to <em>state</em>. <em>state</em> must be a decoder state
  825. returned by <a class="reference internal" href="#codecs.IncrementalDecoder.getstate" title="codecs.IncrementalDecoder.getstate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getstate()</span></code></a>.</p>
  826. </dd></dl>
  827. </dd></dl>
  828. </section>
  829. </section>
  830. <section id="stream-encoding-and-decoding">
  831. <h3>Stream Encoding and Decoding<a class="headerlink" href="#stream-encoding-and-decoding" title="Link to this heading">¶</a></h3>
  832. <p>The <a class="reference internal" href="#codecs.StreamWriter" title="codecs.StreamWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamWriter</span></code></a> and <a class="reference internal" href="#codecs.StreamReader" title="codecs.StreamReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code></a> classes provide generic
  833. working interfaces which can be used to implement new encoding submodules very
  834. easily. See <code class="xref py py-mod docutils literal notranslate"><span class="pre">encodings.utf_8</span></code> for an example of how this is done.</p>
  835. <section id="streamwriter-objects">
  836. <span id="stream-writer-objects"></span><h4>StreamWriter Objects<a class="headerlink" href="#streamwriter-objects" title="Link to this heading">¶</a></h4>
  837. <p>The <a class="reference internal" href="#codecs.StreamWriter" title="codecs.StreamWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamWriter</span></code></a> class is a subclass of <a class="reference internal" href="#codecs.Codec" title="codecs.Codec"><code class="xref py py-class docutils literal notranslate"><span class="pre">Codec</span></code></a> and defines the
  838. following methods which every stream writer must define in order to be
  839. compatible with the Python codec registry.</p>
  840. <dl class="py class">
  841. <dt class="sig sig-object py" id="codecs.StreamWriter">
  842. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">StreamWriter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">stream</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'strict'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.StreamWriter" title="Link to this definition">¶</a></dt>
  843. <dd><p>Constructor for a <a class="reference internal" href="#codecs.StreamWriter" title="codecs.StreamWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamWriter</span></code></a> instance.</p>
  844. <p>All stream writers must provide this constructor interface. They are free to add
  845. additional keyword arguments, but only the ones defined here are used by the
  846. Python codec registry.</p>
  847. <p>The <em>stream</em> argument must be a file-like object open for writing
  848. text or binary data, as appropriate for the specific codec.</p>
  849. <p>The <a class="reference internal" href="#codecs.StreamWriter" title="codecs.StreamWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamWriter</span></code></a> may implement different error handling schemes by
  850. providing the <em>errors</em> keyword argument. See <a class="reference internal" href="#error-handlers"><span class="std std-ref">Error Handlers</span></a> for
  851. the standard error handlers the underlying stream codec may support.</p>
  852. <p>The <em>errors</em> argument will be assigned to an attribute of the same name.
  853. Assigning to this attribute makes it possible to switch between different error
  854. handling strategies during the lifetime of the <a class="reference internal" href="#codecs.StreamWriter" title="codecs.StreamWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamWriter</span></code></a> object.</p>
  855. <dl class="py method">
  856. <dt class="sig sig-object py" id="codecs.StreamWriter.write">
  857. <span class="sig-name descname"><span class="pre">write</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.StreamWriter.write" title="Link to this definition">¶</a></dt>
  858. <dd><p>Writes the object’s contents encoded to the stream.</p>
  859. </dd></dl>
  860. <dl class="py method">
  861. <dt class="sig sig-object py" id="codecs.StreamWriter.writelines">
  862. <span class="sig-name descname"><span class="pre">writelines</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">list</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.StreamWriter.writelines" title="Link to this definition">¶</a></dt>
  863. <dd><p>Writes the concatenated iterable of strings to the stream (possibly by reusing
  864. the <a class="reference internal" href="#codecs.StreamWriter.write" title="codecs.StreamWriter.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a> method). Infinite or
  865. very large iterables are not supported. The standard bytes-to-bytes codecs
  866. do not support this method.</p>
  867. </dd></dl>
  868. <dl class="py method">
  869. <dt class="sig sig-object py" id="codecs.StreamWriter.reset">
  870. <span class="sig-name descname"><span class="pre">reset</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#codecs.StreamWriter.reset" title="Link to this definition">¶</a></dt>
  871. <dd><p>Resets the codec buffers used for keeping internal state.</p>
  872. <p>Calling this method should ensure that the data on the output is put into
  873. a clean state that allows appending of new fresh data without having to
  874. rescan the whole stream to recover state.</p>
  875. </dd></dl>
  876. </dd></dl>
  877. <p>In addition to the above methods, the <a class="reference internal" href="#codecs.StreamWriter" title="codecs.StreamWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamWriter</span></code></a> must also inherit
  878. all other methods and attributes from the underlying stream.</p>
  879. </section>
  880. <section id="streamreader-objects">
  881. <span id="stream-reader-objects"></span><h4>StreamReader Objects<a class="headerlink" href="#streamreader-objects" title="Link to this heading">¶</a></h4>
  882. <p>The <a class="reference internal" href="#codecs.StreamReader" title="codecs.StreamReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code></a> class is a subclass of <a class="reference internal" href="#codecs.Codec" title="codecs.Codec"><code class="xref py py-class docutils literal notranslate"><span class="pre">Codec</span></code></a> and defines the
  883. following methods which every stream reader must define in order to be
  884. compatible with the Python codec registry.</p>
  885. <dl class="py class">
  886. <dt class="sig sig-object py" id="codecs.StreamReader">
  887. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">StreamReader</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">stream</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'strict'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.StreamReader" title="Link to this definition">¶</a></dt>
  888. <dd><p>Constructor for a <a class="reference internal" href="#codecs.StreamReader" title="codecs.StreamReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code></a> instance.</p>
  889. <p>All stream readers must provide this constructor interface. They are free to add
  890. additional keyword arguments, but only the ones defined here are used by the
  891. Python codec registry.</p>
  892. <p>The <em>stream</em> argument must be a file-like object open for reading
  893. text or binary data, as appropriate for the specific codec.</p>
  894. <p>The <a class="reference internal" href="#codecs.StreamReader" title="codecs.StreamReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code></a> may implement different error handling schemes by
  895. providing the <em>errors</em> keyword argument. See <a class="reference internal" href="#error-handlers"><span class="std std-ref">Error Handlers</span></a> for
  896. the standard error handlers the underlying stream codec may support.</p>
  897. <p>The <em>errors</em> argument will be assigned to an attribute of the same name.
  898. Assigning to this attribute makes it possible to switch between different error
  899. handling strategies during the lifetime of the <a class="reference internal" href="#codecs.StreamReader" title="codecs.StreamReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code></a> object.</p>
  900. <p>The set of allowed values for the <em>errors</em> argument can be extended with
  901. <a class="reference internal" href="#codecs.register_error" title="codecs.register_error"><code class="xref py py-func docutils literal notranslate"><span class="pre">register_error()</span></code></a>.</p>
  902. <dl class="py method">
  903. <dt class="sig sig-object py" id="codecs.StreamReader.read">
  904. <span class="sig-name descname"><span class="pre">read</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">chars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">firstline</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.StreamReader.read" title="Link to this definition">¶</a></dt>
  905. <dd><p>Decodes data from the stream and returns the resulting object.</p>
  906. <p>The <em>chars</em> argument indicates the number of decoded
  907. code points or bytes to return. The <a class="reference internal" href="#codecs.StreamReader.read" title="codecs.StreamReader.read"><code class="xref py py-func docutils literal notranslate"><span class="pre">read()</span></code></a> method will
  908. never return more data than requested, but it might return less,
  909. if there is not enough available.</p>
  910. <p>The <em>size</em> argument indicates the approximate maximum
  911. number of encoded bytes or code points to read
  912. for decoding. The decoder can modify this setting as
  913. appropriate. The default value -1 indicates to read and decode as much as
  914. possible. This parameter is intended to
  915. prevent having to decode huge files in one step.</p>
  916. <p>The <em>firstline</em> flag indicates that
  917. it would be sufficient to only return the first
  918. line, if there are decoding errors on later lines.</p>
  919. <p>The method should use a greedy read strategy meaning that it should read
  920. as much data as is allowed within the definition of the encoding and the
  921. given size, e.g. if optional encoding endings or state markers are
  922. available on the stream, these should be read too.</p>
  923. </dd></dl>
  924. <dl class="py method">
  925. <dt class="sig sig-object py" id="codecs.StreamReader.readline">
  926. <span class="sig-name descname"><span class="pre">readline</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">keepends</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.StreamReader.readline" title="Link to this definition">¶</a></dt>
  927. <dd><p>Read one line from the input stream and return the decoded data.</p>
  928. <p><em>size</em>, if given, is passed as size argument to the stream’s
  929. <a class="reference internal" href="#codecs.StreamReader.read" title="codecs.StreamReader.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> method.</p>
  930. <p>If <em>keepends</em> is false line-endings will be stripped from the lines
  931. returned.</p>
  932. </dd></dl>
  933. <dl class="py method">
  934. <dt class="sig sig-object py" id="codecs.StreamReader.readlines">
  935. <span class="sig-name descname"><span class="pre">readlines</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sizehint</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">keepends</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.StreamReader.readlines" title="Link to this definition">¶</a></dt>
  936. <dd><p>Read all lines available on the input stream and return them as a list of
  937. lines.</p>
  938. <p>Line-endings are implemented using the codec’s <a class="reference internal" href="#codecs.decode" title="codecs.decode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decode()</span></code></a> method and
  939. are included in the list entries if <em>keepends</em> is true.</p>
  940. <p><em>sizehint</em>, if given, is passed as the <em>size</em> argument to the stream’s
  941. <a class="reference internal" href="#codecs.StreamReader.read" title="codecs.StreamReader.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> method.</p>
  942. </dd></dl>
  943. <dl class="py method">
  944. <dt class="sig sig-object py" id="codecs.StreamReader.reset">
  945. <span class="sig-name descname"><span class="pre">reset</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#codecs.StreamReader.reset" title="Link to this definition">¶</a></dt>
  946. <dd><p>Resets the codec buffers used for keeping internal state.</p>
  947. <p>Note that no stream repositioning should take place. This method is
  948. primarily intended to be able to recover from decoding errors.</p>
  949. </dd></dl>
  950. </dd></dl>
  951. <p>In addition to the above methods, the <a class="reference internal" href="#codecs.StreamReader" title="codecs.StreamReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code></a> must also inherit
  952. all other methods and attributes from the underlying stream.</p>
  953. </section>
  954. <section id="streamreaderwriter-objects">
  955. <span id="stream-reader-writer"></span><h4>StreamReaderWriter Objects<a class="headerlink" href="#streamreaderwriter-objects" title="Link to this heading">¶</a></h4>
  956. <p>The <a class="reference internal" href="#codecs.StreamReaderWriter" title="codecs.StreamReaderWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReaderWriter</span></code></a> is a convenience class that allows wrapping
  957. streams which work in both read and write modes.</p>
  958. <p>The design is such that one can use the factory functions returned by the
  959. <a class="reference internal" href="#codecs.lookup" title="codecs.lookup"><code class="xref py py-func docutils literal notranslate"><span class="pre">lookup()</span></code></a> function to construct the instance.</p>
  960. <dl class="py class">
  961. <dt class="sig sig-object py" id="codecs.StreamReaderWriter">
  962. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">StreamReaderWriter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">stream</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">Reader</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">Writer</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'strict'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.StreamReaderWriter" title="Link to this definition">¶</a></dt>
  963. <dd><p>Creates a <a class="reference internal" href="#codecs.StreamReaderWriter" title="codecs.StreamReaderWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReaderWriter</span></code></a> instance. <em>stream</em> must be a file-like
  964. object. <em>Reader</em> and <em>Writer</em> must be factory functions or classes providing the
  965. <a class="reference internal" href="#codecs.StreamReader" title="codecs.StreamReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code></a> and <a class="reference internal" href="#codecs.StreamWriter" title="codecs.StreamWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamWriter</span></code></a> interface resp. Error handling
  966. is done in the same way as defined for the stream readers and writers.</p>
  967. </dd></dl>
  968. <p><a class="reference internal" href="#codecs.StreamReaderWriter" title="codecs.StreamReaderWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReaderWriter</span></code></a> instances define the combined interfaces of
  969. <a class="reference internal" href="#codecs.StreamReader" title="codecs.StreamReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code></a> and <a class="reference internal" href="#codecs.StreamWriter" title="codecs.StreamWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamWriter</span></code></a> classes. They inherit all other
  970. methods and attributes from the underlying stream.</p>
  971. </section>
  972. <section id="streamrecoder-objects">
  973. <span id="stream-recoder-objects"></span><h4>StreamRecoder Objects<a class="headerlink" href="#streamrecoder-objects" title="Link to this heading">¶</a></h4>
  974. <p>The <a class="reference internal" href="#codecs.StreamRecoder" title="codecs.StreamRecoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamRecoder</span></code></a> translates data from one encoding to another,
  975. which is sometimes useful when dealing with different encoding environments.</p>
  976. <p>The design is such that one can use the factory functions returned by the
  977. <a class="reference internal" href="#codecs.lookup" title="codecs.lookup"><code class="xref py py-func docutils literal notranslate"><span class="pre">lookup()</span></code></a> function to construct the instance.</p>
  978. <dl class="py class">
  979. <dt class="sig sig-object py" id="codecs.StreamRecoder">
  980. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">codecs.</span></span><span class="sig-name descname"><span class="pre">StreamRecoder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">stream</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encode</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">decode</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">Reader</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">Writer</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'strict'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#codecs.StreamRecoder" title="Link to this definition">¶</a></dt>
  981. <dd><p>Creates a <a class="reference internal" href="#codecs.StreamRecoder" title="codecs.StreamRecoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamRecoder</span></code></a> instance which implements a two-way conversion:
  982. <em>encode</em> and <em>decode</em> work on the frontend — the data visible to
  983. code calling <a class="reference internal" href="#codecs.StreamReader.read" title="codecs.StreamReader.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> and <a class="reference internal" href="#codecs.StreamWriter.write" title="codecs.StreamWriter.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a>,
  984. while <em>Reader</em> and <em>Writer</em>
  985. work on the backend — the data in <em>stream</em>.</p>
  986. <p>You can use these objects to do transparent transcodings, e.g., from Latin-1
  987. to UTF-8 and back.</p>
  988. <p>The <em>stream</em> argument must be a file-like object.</p>
  989. <p>The <em>encode</em> and <em>decode</em> arguments must
  990. adhere to the <a class="reference internal" href="#codecs.Codec" title="codecs.Codec"><code class="xref py py-class docutils literal notranslate"><span class="pre">Codec</span></code></a> interface. <em>Reader</em> and
  991. <em>Writer</em> must be factory functions or classes providing objects of the
  992. <a class="reference internal" href="#codecs.StreamReader" title="codecs.StreamReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code></a> and <a class="reference internal" href="#codecs.StreamWriter" title="codecs.StreamWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamWriter</span></code></a> interface respectively.</p>
  993. <p>Error handling is done in the same way as defined for the stream readers and
  994. writers.</p>
  995. </dd></dl>
  996. <p><a class="reference internal" href="#codecs.StreamRecoder" title="codecs.StreamRecoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamRecoder</span></code></a> instances define the combined interfaces of
  997. <a class="reference internal" href="#codecs.StreamReader" title="codecs.StreamReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamReader</span></code></a> and <a class="reference internal" href="#codecs.StreamWriter" title="codecs.StreamWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamWriter</span></code></a> classes. They inherit all other
  998. methods and attributes from the underlying stream.</p>
  999. </section>
  1000. </section>
  1001. </section>
  1002. <section id="encodings-and-unicode">
  1003. <span id="encodings-overview"></span><h2>Encodings and Unicode<a class="headerlink" href="#encodings-and-unicode" title="Link to this heading">¶</a></h2>
  1004. <p>Strings are stored internally as sequences of code points in
  1005. range <code class="docutils literal notranslate"><span class="pre">U+0000</span></code>–<code class="docutils literal notranslate"><span class="pre">U+10FFFF</span></code>. (See <span class="target" id="index-5"></span><a class="pep reference external" href="https://peps.python.org/pep-0393/"><strong>PEP 393</strong></a> for
  1006. more details about the implementation.)
  1007. Once a string object is used outside of CPU and memory, endianness
  1008. and how these arrays are stored as bytes become an issue. As with other
  1009. codecs, serialising a string into a sequence of bytes is known as <em>encoding</em>,
  1010. and recreating the string from the sequence of bytes is known as <em>decoding</em>.
  1011. There are a variety of different text serialisation codecs, which are
  1012. collectivity referred to as <a class="reference internal" href="../glossary.html#term-text-encoding"><span class="xref std std-term">text encodings</span></a>.</p>
  1013. <p>The simplest text encoding (called <code class="docutils literal notranslate"><span class="pre">'latin-1'</span></code> or <code class="docutils literal notranslate"><span class="pre">'iso-8859-1'</span></code>) maps
  1014. the code points 0–255 to the bytes <code class="docutils literal notranslate"><span class="pre">0x0</span></code>–<code class="docutils literal notranslate"><span class="pre">0xff</span></code>, which means that a string
  1015. object that contains code points above <code class="docutils literal notranslate"><span class="pre">U+00FF</span></code> can’t be encoded with this
  1016. codec. Doing so will raise a <a class="reference internal" href="exceptions.html#UnicodeEncodeError" title="UnicodeEncodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeEncodeError</span></code></a> that looks
  1017. like the following (although the details of the error message may differ):
  1018. <code class="docutils literal notranslate"><span class="pre">UnicodeEncodeError:</span> <span class="pre">'latin-1'</span> <span class="pre">codec</span> <span class="pre">can't</span> <span class="pre">encode</span> <span class="pre">character</span> <span class="pre">'\u1234'</span> <span class="pre">in</span>
  1019. <span class="pre">position</span> <span class="pre">3:</span> <span class="pre">ordinal</span> <span class="pre">not</span> <span class="pre">in</span> <span class="pre">range(256)</span></code>.</p>
  1020. <p>There’s another group of encodings (the so called charmap encodings) that choose
  1021. a different subset of all Unicode code points and how these code points are
  1022. mapped to the bytes <code class="docutils literal notranslate"><span class="pre">0x0</span></code>–<code class="docutils literal notranslate"><span class="pre">0xff</span></code>. To see how this is done simply open
  1023. e.g. <code class="file docutils literal notranslate"><span class="pre">encodings/cp1252.py</span></code> (which is an encoding that is used primarily on
  1024. Windows). There’s a string constant with 256 characters that shows you which
  1025. character is mapped to which byte value.</p>
  1026. <p>All of these encodings can only encode 256 of the 1114112 code points
  1027. defined in Unicode. A simple and straightforward way that can store each Unicode
  1028. code point, is to store each code point as four consecutive bytes. There are two
  1029. possibilities: store the bytes in big endian or in little endian order. These
  1030. two encodings are called <code class="docutils literal notranslate"><span class="pre">UTF-32-BE</span></code> and <code class="docutils literal notranslate"><span class="pre">UTF-32-LE</span></code> respectively. Their
  1031. disadvantage is that if e.g. you use <code class="docutils literal notranslate"><span class="pre">UTF-32-BE</span></code> on a little endian machine you
  1032. will always have to swap bytes on encoding and decoding. <code class="docutils literal notranslate"><span class="pre">UTF-32</span></code> avoids this
  1033. problem: bytes will always be in natural endianness. When these bytes are read
  1034. by a CPU with a different endianness, then bytes have to be swapped though. To
  1035. be able to detect the endianness of a <code class="docutils literal notranslate"><span class="pre">UTF-16</span></code> or <code class="docutils literal notranslate"><span class="pre">UTF-32</span></code> byte sequence,
  1036. there’s the so called BOM (“Byte Order Mark”). This is the Unicode character
  1037. <code class="docutils literal notranslate"><span class="pre">U+FEFF</span></code>. This character can be prepended to every <code class="docutils literal notranslate"><span class="pre">UTF-16</span></code> or <code class="docutils literal notranslate"><span class="pre">UTF-32</span></code>
  1038. byte sequence. The byte swapped version of this character (<code class="docutils literal notranslate"><span class="pre">0xFFFE</span></code>) is an
  1039. illegal character that may not appear in a Unicode text. So when the
  1040. first character in a <code class="docutils literal notranslate"><span class="pre">UTF-16</span></code> or <code class="docutils literal notranslate"><span class="pre">UTF-32</span></code> byte sequence
  1041. appears to be a <code class="docutils literal notranslate"><span class="pre">U+FFFE</span></code> the bytes have to be swapped on decoding.
  1042. Unfortunately the character <code class="docutils literal notranslate"><span class="pre">U+FEFF</span></code> had a second purpose as
  1043. a <code class="docutils literal notranslate"><span class="pre">ZERO</span> <span class="pre">WIDTH</span> <span class="pre">NO-BREAK</span> <span class="pre">SPACE</span></code>: a character that has no width and doesn’t allow
  1044. a word to be split. It can e.g. be used to give hints to a ligature algorithm.
  1045. With Unicode 4.0 using <code class="docutils literal notranslate"><span class="pre">U+FEFF</span></code> as a <code class="docutils literal notranslate"><span class="pre">ZERO</span> <span class="pre">WIDTH</span> <span class="pre">NO-BREAK</span> <span class="pre">SPACE</span></code> has been
  1046. deprecated (with <code class="docutils literal notranslate"><span class="pre">U+2060</span></code> (<code class="docutils literal notranslate"><span class="pre">WORD</span> <span class="pre">JOINER</span></code>) assuming this role). Nevertheless
  1047. Unicode software still must be able to handle <code class="docutils literal notranslate"><span class="pre">U+FEFF</span></code> in both roles: as a BOM
  1048. it’s a device to determine the storage layout of the encoded bytes, and vanishes
  1049. once the byte sequence has been decoded into a string; as a <code class="docutils literal notranslate"><span class="pre">ZERO</span> <span class="pre">WIDTH</span>
  1050. <span class="pre">NO-BREAK</span> <span class="pre">SPACE</span></code> it’s a normal character that will be decoded like any other.</p>
  1051. <p>There’s another encoding that is able to encode the full range of Unicode
  1052. characters: UTF-8. UTF-8 is an 8-bit encoding, which means there are no issues
  1053. with byte order in UTF-8. Each byte in a UTF-8 byte sequence consists of two
  1054. parts: marker bits (the most significant bits) and payload bits. The marker bits
  1055. are a sequence of zero to four <code class="docutils literal notranslate"><span class="pre">1</span></code> bits followed by a <code class="docutils literal notranslate"><span class="pre">0</span></code> bit. Unicode characters are
  1056. encoded like this (with x being payload bits, which when concatenated give the
  1057. Unicode character):</p>
  1058. <table class="docutils align-default">
  1059. <thead>
  1060. <tr class="row-odd"><th class="head"><p>Range</p></th>
  1061. <th class="head"><p>Encoding</p></th>
  1062. </tr>
  1063. </thead>
  1064. <tbody>
  1065. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">U-00000000</span></code> … <code class="docutils literal notranslate"><span class="pre">U-0000007F</span></code></p></td>
  1066. <td><p>0xxxxxxx</p></td>
  1067. </tr>
  1068. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">U-00000080</span></code> … <code class="docutils literal notranslate"><span class="pre">U-000007FF</span></code></p></td>
  1069. <td><p>110xxxxx 10xxxxxx</p></td>
  1070. </tr>
  1071. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">U-00000800</span></code> … <code class="docutils literal notranslate"><span class="pre">U-0000FFFF</span></code></p></td>
  1072. <td><p>1110xxxx 10xxxxxx 10xxxxxx</p></td>
  1073. </tr>
  1074. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">U-00010000</span></code> … <code class="docutils literal notranslate"><span class="pre">U-0010FFFF</span></code></p></td>
  1075. <td><p>11110xxx 10xxxxxx 10xxxxxx 10xxxxxx</p></td>
  1076. </tr>
  1077. </tbody>
  1078. </table>
  1079. <p>The least significant bit of the Unicode character is the rightmost x bit.</p>
  1080. <p>As UTF-8 is an 8-bit encoding no BOM is required and any <code class="docutils literal notranslate"><span class="pre">U+FEFF</span></code> character in
  1081. the decoded string (even if it’s the first character) is treated as a <code class="docutils literal notranslate"><span class="pre">ZERO</span>
  1082. <span class="pre">WIDTH</span> <span class="pre">NO-BREAK</span> <span class="pre">SPACE</span></code>.</p>
  1083. <p>Without external information it’s impossible to reliably determine which
  1084. encoding was used for encoding a string. Each charmap encoding can
  1085. decode any random byte sequence. However that’s not possible with UTF-8, as
  1086. UTF-8 byte sequences have a structure that doesn’t allow arbitrary byte
  1087. sequences. To increase the reliability with which a UTF-8 encoding can be
  1088. detected, Microsoft invented a variant of UTF-8 (that Python calls
  1089. <code class="docutils literal notranslate"><span class="pre">&quot;utf-8-sig&quot;</span></code>) for its Notepad program: Before any of the Unicode characters
  1090. is written to the file, a UTF-8 encoded BOM (which looks like this as a byte
  1091. sequence: <code class="docutils literal notranslate"><span class="pre">0xef</span></code>, <code class="docutils literal notranslate"><span class="pre">0xbb</span></code>, <code class="docutils literal notranslate"><span class="pre">0xbf</span></code>) is written. As it’s rather improbable
  1092. that any charmap encoded file starts with these byte values (which would e.g.
  1093. map to</p>
  1094. <blockquote>
  1095. <div><div class="line-block">
  1096. <div class="line">LATIN SMALL LETTER I WITH DIAERESIS</div>
  1097. <div class="line">RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK</div>
  1098. <div class="line">INVERTED QUESTION MARK</div>
  1099. </div>
  1100. </div></blockquote>
  1101. <p>in iso-8859-1), this increases the probability that a <code class="docutils literal notranslate"><span class="pre">utf-8-sig</span></code> encoding can be
  1102. correctly guessed from the byte sequence. So here the BOM is not used to be able
  1103. to determine the byte order used for generating the byte sequence, but as a
  1104. signature that helps in guessing the encoding. On encoding the utf-8-sig codec
  1105. will write <code class="docutils literal notranslate"><span class="pre">0xef</span></code>, <code class="docutils literal notranslate"><span class="pre">0xbb</span></code>, <code class="docutils literal notranslate"><span class="pre">0xbf</span></code> as the first three bytes to the file. On
  1106. decoding <code class="docutils literal notranslate"><span class="pre">utf-8-sig</span></code> will skip those three bytes if they appear as the first
  1107. three bytes in the file. In UTF-8, the use of the BOM is discouraged and
  1108. should generally be avoided.</p>
  1109. </section>
  1110. <section id="standard-encodings">
  1111. <span id="id3"></span><h2>Standard Encodings<a class="headerlink" href="#standard-encodings" title="Link to this heading">¶</a></h2>
  1112. <p>Python comes with a number of codecs built-in, either implemented as C functions
  1113. or with dictionaries as mapping tables. The following table lists the codecs by
  1114. name, together with a few common aliases, and the languages for which the
  1115. encoding is likely used. Neither the list of aliases nor the list of languages
  1116. is meant to be exhaustive. Notice that spelling alternatives that only differ in
  1117. case or use a hyphen instead of an underscore are also valid aliases; therefore,
  1118. e.g. <code class="docutils literal notranslate"><span class="pre">'utf-8'</span></code> is a valid alias for the <code class="docutils literal notranslate"><span class="pre">'utf_8'</span></code> codec.</p>
  1119. <div class="impl-detail compound">
  1120. <p><strong>CPython implementation detail:</strong> Some common encodings can bypass the codecs lookup machinery to
  1121. improve performance. These optimization opportunities are only
  1122. recognized by CPython for a limited set of (case insensitive)
  1123. aliases: utf-8, utf8, latin-1, latin1, iso-8859-1, iso8859-1, mbcs
  1124. (Windows only), ascii, us-ascii, utf-16, utf16, utf-32, utf32, and
  1125. the same using underscores instead of dashes. Using alternative
  1126. aliases for these encodings may result in slower execution.</p>
  1127. <div class="versionchanged">
  1128. <p><span class="versionmodified changed">Changed in version 3.6: </span>Optimization opportunity recognized for us-ascii.</p>
  1129. </div>
  1130. </div>
  1131. <p>Many of the character sets support the same languages. They vary in individual
  1132. characters (e.g. whether the EURO SIGN is supported or not), and in the
  1133. assignment of characters to code positions. For the European languages in
  1134. particular, the following variants typically exist:</p>
  1135. <ul class="simple">
  1136. <li><p>an ISO 8859 codeset</p></li>
  1137. <li><p>a Microsoft Windows code page, which is typically derived from an 8859 codeset,
  1138. but replaces control characters with additional graphic characters</p></li>
  1139. <li><p>an IBM EBCDIC code page</p></li>
  1140. <li><p>an IBM PC code page, which is ASCII compatible</p></li>
  1141. </ul>
  1142. <table class="docutils align-default">
  1143. <thead>
  1144. <tr class="row-odd"><th class="head"><p>Codec</p></th>
  1145. <th class="head"><p>Aliases</p></th>
  1146. <th class="head"><p>Languages</p></th>
  1147. </tr>
  1148. </thead>
  1149. <tbody>
  1150. <tr class="row-even"><td><p>ascii</p></td>
  1151. <td><p>646, us-ascii</p></td>
  1152. <td><p>English</p></td>
  1153. </tr>
  1154. <tr class="row-odd"><td><p>big5</p></td>
  1155. <td><p>big5-tw, csbig5</p></td>
  1156. <td><p>Traditional Chinese</p></td>
  1157. </tr>
  1158. <tr class="row-even"><td><p>big5hkscs</p></td>
  1159. <td><p>big5-hkscs, hkscs</p></td>
  1160. <td><p>Traditional Chinese</p></td>
  1161. </tr>
  1162. <tr class="row-odd"><td><p>cp037</p></td>
  1163. <td><p>IBM037, IBM039</p></td>
  1164. <td><p>English</p></td>
  1165. </tr>
  1166. <tr class="row-even"><td><p>cp273</p></td>
  1167. <td><p>273, IBM273, csIBM273</p></td>
  1168. <td><p>German</p>
  1169. <div class="versionadded">
  1170. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1171. </div>
  1172. </td>
  1173. </tr>
  1174. <tr class="row-odd"><td><p>cp424</p></td>
  1175. <td><p>EBCDIC-CP-HE, IBM424</p></td>
  1176. <td><p>Hebrew</p></td>
  1177. </tr>
  1178. <tr class="row-even"><td><p>cp437</p></td>
  1179. <td><p>437, IBM437</p></td>
  1180. <td><p>English</p></td>
  1181. </tr>
  1182. <tr class="row-odd"><td><p>cp500</p></td>
  1183. <td><p>EBCDIC-CP-BE, EBCDIC-CP-CH,
  1184. IBM500</p></td>
  1185. <td><p>Western Europe</p></td>
  1186. </tr>
  1187. <tr class="row-even"><td><p>cp720</p></td>
  1188. <td></td>
  1189. <td><p>Arabic</p></td>
  1190. </tr>
  1191. <tr class="row-odd"><td><p>cp737</p></td>
  1192. <td></td>
  1193. <td><p>Greek</p></td>
  1194. </tr>
  1195. <tr class="row-even"><td><p>cp775</p></td>
  1196. <td><p>IBM775</p></td>
  1197. <td><p>Baltic languages</p></td>
  1198. </tr>
  1199. <tr class="row-odd"><td><p>cp850</p></td>
  1200. <td><p>850, IBM850</p></td>
  1201. <td><p>Western Europe</p></td>
  1202. </tr>
  1203. <tr class="row-even"><td><p>cp852</p></td>
  1204. <td><p>852, IBM852</p></td>
  1205. <td><p>Central and Eastern Europe</p></td>
  1206. </tr>
  1207. <tr class="row-odd"><td><p>cp855</p></td>
  1208. <td><p>855, IBM855</p></td>
  1209. <td><p>Bulgarian, Byelorussian,
  1210. Macedonian, Russian, Serbian</p></td>
  1211. </tr>
  1212. <tr class="row-even"><td><p>cp856</p></td>
  1213. <td></td>
  1214. <td><p>Hebrew</p></td>
  1215. </tr>
  1216. <tr class="row-odd"><td><p>cp857</p></td>
  1217. <td><p>857, IBM857</p></td>
  1218. <td><p>Turkish</p></td>
  1219. </tr>
  1220. <tr class="row-even"><td><p>cp858</p></td>
  1221. <td><p>858, IBM858</p></td>
  1222. <td><p>Western Europe</p></td>
  1223. </tr>
  1224. <tr class="row-odd"><td><p>cp860</p></td>
  1225. <td><p>860, IBM860</p></td>
  1226. <td><p>Portuguese</p></td>
  1227. </tr>
  1228. <tr class="row-even"><td><p>cp861</p></td>
  1229. <td><p>861, CP-IS, IBM861</p></td>
  1230. <td><p>Icelandic</p></td>
  1231. </tr>
  1232. <tr class="row-odd"><td><p>cp862</p></td>
  1233. <td><p>862, IBM862</p></td>
  1234. <td><p>Hebrew</p></td>
  1235. </tr>
  1236. <tr class="row-even"><td><p>cp863</p></td>
  1237. <td><p>863, IBM863</p></td>
  1238. <td><p>Canadian</p></td>
  1239. </tr>
  1240. <tr class="row-odd"><td><p>cp864</p></td>
  1241. <td><p>IBM864</p></td>
  1242. <td><p>Arabic</p></td>
  1243. </tr>
  1244. <tr class="row-even"><td><p>cp865</p></td>
  1245. <td><p>865, IBM865</p></td>
  1246. <td><p>Danish, Norwegian</p></td>
  1247. </tr>
  1248. <tr class="row-odd"><td><p>cp866</p></td>
  1249. <td><p>866, IBM866</p></td>
  1250. <td><p>Russian</p></td>
  1251. </tr>
  1252. <tr class="row-even"><td><p>cp869</p></td>
  1253. <td><p>869, CP-GR, IBM869</p></td>
  1254. <td><p>Greek</p></td>
  1255. </tr>
  1256. <tr class="row-odd"><td><p>cp874</p></td>
  1257. <td></td>
  1258. <td><p>Thai</p></td>
  1259. </tr>
  1260. <tr class="row-even"><td><p>cp875</p></td>
  1261. <td></td>
  1262. <td><p>Greek</p></td>
  1263. </tr>
  1264. <tr class="row-odd"><td><p>cp932</p></td>
  1265. <td><p>932, ms932, mskanji, ms-kanji</p></td>
  1266. <td><p>Japanese</p></td>
  1267. </tr>
  1268. <tr class="row-even"><td><p>cp949</p></td>
  1269. <td><p>949, ms949, uhc</p></td>
  1270. <td><p>Korean</p></td>
  1271. </tr>
  1272. <tr class="row-odd"><td><p>cp950</p></td>
  1273. <td><p>950, ms950</p></td>
  1274. <td><p>Traditional Chinese</p></td>
  1275. </tr>
  1276. <tr class="row-even"><td><p>cp1006</p></td>
  1277. <td></td>
  1278. <td><p>Urdu</p></td>
  1279. </tr>
  1280. <tr class="row-odd"><td><p>cp1026</p></td>
  1281. <td><p>ibm1026</p></td>
  1282. <td><p>Turkish</p></td>
  1283. </tr>
  1284. <tr class="row-even"><td><p>cp1125</p></td>
  1285. <td><p>1125, ibm1125, cp866u, ruscii</p></td>
  1286. <td><p>Ukrainian</p>
  1287. <div class="versionadded">
  1288. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1289. </div>
  1290. </td>
  1291. </tr>
  1292. <tr class="row-odd"><td><p>cp1140</p></td>
  1293. <td><p>ibm1140</p></td>
  1294. <td><p>Western Europe</p></td>
  1295. </tr>
  1296. <tr class="row-even"><td><p>cp1250</p></td>
  1297. <td><p>windows-1250</p></td>
  1298. <td><p>Central and Eastern Europe</p></td>
  1299. </tr>
  1300. <tr class="row-odd"><td><p>cp1251</p></td>
  1301. <td><p>windows-1251</p></td>
  1302. <td><p>Bulgarian, Byelorussian,
  1303. Macedonian, Russian, Serbian</p></td>
  1304. </tr>
  1305. <tr class="row-even"><td><p>cp1252</p></td>
  1306. <td><p>windows-1252</p></td>
  1307. <td><p>Western Europe</p></td>
  1308. </tr>
  1309. <tr class="row-odd"><td><p>cp1253</p></td>
  1310. <td><p>windows-1253</p></td>
  1311. <td><p>Greek</p></td>
  1312. </tr>
  1313. <tr class="row-even"><td><p>cp1254</p></td>
  1314. <td><p>windows-1254</p></td>
  1315. <td><p>Turkish</p></td>
  1316. </tr>
  1317. <tr class="row-odd"><td><p>cp1255</p></td>
  1318. <td><p>windows-1255</p></td>
  1319. <td><p>Hebrew</p></td>
  1320. </tr>
  1321. <tr class="row-even"><td><p>cp1256</p></td>
  1322. <td><p>windows-1256</p></td>
  1323. <td><p>Arabic</p></td>
  1324. </tr>
  1325. <tr class="row-odd"><td><p>cp1257</p></td>
  1326. <td><p>windows-1257</p></td>
  1327. <td><p>Baltic languages</p></td>
  1328. </tr>
  1329. <tr class="row-even"><td><p>cp1258</p></td>
  1330. <td><p>windows-1258</p></td>
  1331. <td><p>Vietnamese</p></td>
  1332. </tr>
  1333. <tr class="row-odd"><td><p>euc_jp</p></td>
  1334. <td><p>eucjp, ujis, u-jis</p></td>
  1335. <td><p>Japanese</p></td>
  1336. </tr>
  1337. <tr class="row-even"><td><p>euc_jis_2004</p></td>
  1338. <td><p>jisx0213, eucjis2004</p></td>
  1339. <td><p>Japanese</p></td>
  1340. </tr>
  1341. <tr class="row-odd"><td><p>euc_jisx0213</p></td>
  1342. <td><p>eucjisx0213</p></td>
  1343. <td><p>Japanese</p></td>
  1344. </tr>
  1345. <tr class="row-even"><td><p>euc_kr</p></td>
  1346. <td><p>euckr, korean, ksc5601,
  1347. ks_c-5601, ks_c-5601-1987,
  1348. ksx1001, ks_x-1001</p></td>
  1349. <td><p>Korean</p></td>
  1350. </tr>
  1351. <tr class="row-odd"><td><p>gb2312</p></td>
  1352. <td><p>chinese, csiso58gb231280,
  1353. euc-cn, euccn, eucgb2312-cn,
  1354. gb2312-1980, gb2312-80,
  1355. iso-ir-58</p></td>
  1356. <td><p>Simplified Chinese</p></td>
  1357. </tr>
  1358. <tr class="row-even"><td><p>gbk</p></td>
  1359. <td><p>936, cp936, ms936</p></td>
  1360. <td><p>Unified Chinese</p></td>
  1361. </tr>
  1362. <tr class="row-odd"><td><p>gb18030</p></td>
  1363. <td><p>gb18030-2000</p></td>
  1364. <td><p>Unified Chinese</p></td>
  1365. </tr>
  1366. <tr class="row-even"><td><p>hz</p></td>
  1367. <td><p>hzgb, hz-gb, hz-gb-2312</p></td>
  1368. <td><p>Simplified Chinese</p></td>
  1369. </tr>
  1370. <tr class="row-odd"><td><p>iso2022_jp</p></td>
  1371. <td><p>csiso2022jp, iso2022jp,
  1372. iso-2022-jp</p></td>
  1373. <td><p>Japanese</p></td>
  1374. </tr>
  1375. <tr class="row-even"><td><p>iso2022_jp_1</p></td>
  1376. <td><p>iso2022jp-1, iso-2022-jp-1</p></td>
  1377. <td><p>Japanese</p></td>
  1378. </tr>
  1379. <tr class="row-odd"><td><p>iso2022_jp_2</p></td>
  1380. <td><p>iso2022jp-2, iso-2022-jp-2</p></td>
  1381. <td><p>Japanese, Korean, Simplified
  1382. Chinese, Western Europe, Greek</p></td>
  1383. </tr>
  1384. <tr class="row-even"><td><p>iso2022_jp_2004</p></td>
  1385. <td><p>iso2022jp-2004,
  1386. iso-2022-jp-2004</p></td>
  1387. <td><p>Japanese</p></td>
  1388. </tr>
  1389. <tr class="row-odd"><td><p>iso2022_jp_3</p></td>
  1390. <td><p>iso2022jp-3, iso-2022-jp-3</p></td>
  1391. <td><p>Japanese</p></td>
  1392. </tr>
  1393. <tr class="row-even"><td><p>iso2022_jp_ext</p></td>
  1394. <td><p>iso2022jp-ext, iso-2022-jp-ext</p></td>
  1395. <td><p>Japanese</p></td>
  1396. </tr>
  1397. <tr class="row-odd"><td><p>iso2022_kr</p></td>
  1398. <td><p>csiso2022kr, iso2022kr,
  1399. iso-2022-kr</p></td>
  1400. <td><p>Korean</p></td>
  1401. </tr>
  1402. <tr class="row-even"><td><p>latin_1</p></td>
  1403. <td><p>iso-8859-1, iso8859-1, 8859,
  1404. cp819, latin, latin1, L1</p></td>
  1405. <td><p>Western Europe</p></td>
  1406. </tr>
  1407. <tr class="row-odd"><td><p>iso8859_2</p></td>
  1408. <td><p>iso-8859-2, latin2, L2</p></td>
  1409. <td><p>Central and Eastern Europe</p></td>
  1410. </tr>
  1411. <tr class="row-even"><td><p>iso8859_3</p></td>
  1412. <td><p>iso-8859-3, latin3, L3</p></td>
  1413. <td><p>Esperanto, Maltese</p></td>
  1414. </tr>
  1415. <tr class="row-odd"><td><p>iso8859_4</p></td>
  1416. <td><p>iso-8859-4, latin4, L4</p></td>
  1417. <td><p>Baltic languages</p></td>
  1418. </tr>
  1419. <tr class="row-even"><td><p>iso8859_5</p></td>
  1420. <td><p>iso-8859-5, cyrillic</p></td>
  1421. <td><p>Bulgarian, Byelorussian,
  1422. Macedonian, Russian, Serbian</p></td>
  1423. </tr>
  1424. <tr class="row-odd"><td><p>iso8859_6</p></td>
  1425. <td><p>iso-8859-6, arabic</p></td>
  1426. <td><p>Arabic</p></td>
  1427. </tr>
  1428. <tr class="row-even"><td><p>iso8859_7</p></td>
  1429. <td><p>iso-8859-7, greek, greek8</p></td>
  1430. <td><p>Greek</p></td>
  1431. </tr>
  1432. <tr class="row-odd"><td><p>iso8859_8</p></td>
  1433. <td><p>iso-8859-8, hebrew</p></td>
  1434. <td><p>Hebrew</p></td>
  1435. </tr>
  1436. <tr class="row-even"><td><p>iso8859_9</p></td>
  1437. <td><p>iso-8859-9, latin5, L5</p></td>
  1438. <td><p>Turkish</p></td>
  1439. </tr>
  1440. <tr class="row-odd"><td><p>iso8859_10</p></td>
  1441. <td><p>iso-8859-10, latin6, L6</p></td>
  1442. <td><p>Nordic languages</p></td>
  1443. </tr>
  1444. <tr class="row-even"><td><p>iso8859_11</p></td>
  1445. <td><p>iso-8859-11, thai</p></td>
  1446. <td><p>Thai languages</p></td>
  1447. </tr>
  1448. <tr class="row-odd"><td><p>iso8859_13</p></td>
  1449. <td><p>iso-8859-13, latin7, L7</p></td>
  1450. <td><p>Baltic languages</p></td>
  1451. </tr>
  1452. <tr class="row-even"><td><p>iso8859_14</p></td>
  1453. <td><p>iso-8859-14, latin8, L8</p></td>
  1454. <td><p>Celtic languages</p></td>
  1455. </tr>
  1456. <tr class="row-odd"><td><p>iso8859_15</p></td>
  1457. <td><p>iso-8859-15, latin9, L9</p></td>
  1458. <td><p>Western Europe</p></td>
  1459. </tr>
  1460. <tr class="row-even"><td><p>iso8859_16</p></td>
  1461. <td><p>iso-8859-16, latin10, L10</p></td>
  1462. <td><p>South-Eastern Europe</p></td>
  1463. </tr>
  1464. <tr class="row-odd"><td><p>johab</p></td>
  1465. <td><p>cp1361, ms1361</p></td>
  1466. <td><p>Korean</p></td>
  1467. </tr>
  1468. <tr class="row-even"><td><p>koi8_r</p></td>
  1469. <td></td>
  1470. <td><p>Russian</p></td>
  1471. </tr>
  1472. <tr class="row-odd"><td><p>koi8_t</p></td>
  1473. <td></td>
  1474. <td><p>Tajik</p>
  1475. <div class="versionadded">
  1476. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1477. </div>
  1478. </td>
  1479. </tr>
  1480. <tr class="row-even"><td><p>koi8_u</p></td>
  1481. <td></td>
  1482. <td><p>Ukrainian</p></td>
  1483. </tr>
  1484. <tr class="row-odd"><td><p>kz1048</p></td>
  1485. <td><p>kz_1048, strk1048_2002, rk1048</p></td>
  1486. <td><p>Kazakh</p>
  1487. <div class="versionadded">
  1488. <p><span class="versionmodified added">New in version 3.5.</span></p>
  1489. </div>
  1490. </td>
  1491. </tr>
  1492. <tr class="row-even"><td><p>mac_cyrillic</p></td>
  1493. <td><p>maccyrillic</p></td>
  1494. <td><p>Bulgarian, Byelorussian,
  1495. Macedonian, Russian, Serbian</p></td>
  1496. </tr>
  1497. <tr class="row-odd"><td><p>mac_greek</p></td>
  1498. <td><p>macgreek</p></td>
  1499. <td><p>Greek</p></td>
  1500. </tr>
  1501. <tr class="row-even"><td><p>mac_iceland</p></td>
  1502. <td><p>maciceland</p></td>
  1503. <td><p>Icelandic</p></td>
  1504. </tr>
  1505. <tr class="row-odd"><td><p>mac_latin2</p></td>
  1506. <td><p>maclatin2, maccentraleurope,
  1507. mac_centeuro</p></td>
  1508. <td><p>Central and Eastern Europe</p></td>
  1509. </tr>
  1510. <tr class="row-even"><td><p>mac_roman</p></td>
  1511. <td><p>macroman, macintosh</p></td>
  1512. <td><p>Western Europe</p></td>
  1513. </tr>
  1514. <tr class="row-odd"><td><p>mac_turkish</p></td>
  1515. <td><p>macturkish</p></td>
  1516. <td><p>Turkish</p></td>
  1517. </tr>
  1518. <tr class="row-even"><td><p>ptcp154</p></td>
  1519. <td><p>csptcp154, pt154, cp154,
  1520. cyrillic-asian</p></td>
  1521. <td><p>Kazakh</p></td>
  1522. </tr>
  1523. <tr class="row-odd"><td><p>shift_jis</p></td>
  1524. <td><p>csshiftjis, shiftjis, sjis,
  1525. s_jis</p></td>
  1526. <td><p>Japanese</p></td>
  1527. </tr>
  1528. <tr class="row-even"><td><p>shift_jis_2004</p></td>
  1529. <td><p>shiftjis2004, sjis_2004,
  1530. sjis2004</p></td>
  1531. <td><p>Japanese</p></td>
  1532. </tr>
  1533. <tr class="row-odd"><td><p>shift_jisx0213</p></td>
  1534. <td><p>shiftjisx0213, sjisx0213,
  1535. s_jisx0213</p></td>
  1536. <td><p>Japanese</p></td>
  1537. </tr>
  1538. <tr class="row-even"><td><p>utf_32</p></td>
  1539. <td><p>U32, utf32</p></td>
  1540. <td><p>all languages</p></td>
  1541. </tr>
  1542. <tr class="row-odd"><td><p>utf_32_be</p></td>
  1543. <td><p>UTF-32BE</p></td>
  1544. <td><p>all languages</p></td>
  1545. </tr>
  1546. <tr class="row-even"><td><p>utf_32_le</p></td>
  1547. <td><p>UTF-32LE</p></td>
  1548. <td><p>all languages</p></td>
  1549. </tr>
  1550. <tr class="row-odd"><td><p>utf_16</p></td>
  1551. <td><p>U16, utf16</p></td>
  1552. <td><p>all languages</p></td>
  1553. </tr>
  1554. <tr class="row-even"><td><p>utf_16_be</p></td>
  1555. <td><p>UTF-16BE</p></td>
  1556. <td><p>all languages</p></td>
  1557. </tr>
  1558. <tr class="row-odd"><td><p>utf_16_le</p></td>
  1559. <td><p>UTF-16LE</p></td>
  1560. <td><p>all languages</p></td>
  1561. </tr>
  1562. <tr class="row-even"><td><p>utf_7</p></td>
  1563. <td><p>U7, unicode-1-1-utf-7</p></td>
  1564. <td><p>all languages</p></td>
  1565. </tr>
  1566. <tr class="row-odd"><td><p>utf_8</p></td>
  1567. <td><p>U8, UTF, utf8, cp65001</p></td>
  1568. <td><p>all languages</p></td>
  1569. </tr>
  1570. <tr class="row-even"><td><p>utf_8_sig</p></td>
  1571. <td></td>
  1572. <td><p>all languages</p></td>
  1573. </tr>
  1574. </tbody>
  1575. </table>
  1576. <div class="versionchanged">
  1577. <p><span class="versionmodified changed">Changed in version 3.4: </span>The utf-16* and utf-32* encoders no longer allow surrogate code points
  1578. (<code class="docutils literal notranslate"><span class="pre">U+D800</span></code>–<code class="docutils literal notranslate"><span class="pre">U+DFFF</span></code>) to be encoded.
  1579. The utf-32* decoders no longer decode
  1580. byte sequences that correspond to surrogate code points.</p>
  1581. </div>
  1582. <div class="versionchanged">
  1583. <p><span class="versionmodified changed">Changed in version 3.8: </span><code class="docutils literal notranslate"><span class="pre">cp65001</span></code> is now an alias to <code class="docutils literal notranslate"><span class="pre">utf_8</span></code>.</p>
  1584. </div>
  1585. </section>
  1586. <section id="python-specific-encodings">
  1587. <h2>Python Specific Encodings<a class="headerlink" href="#python-specific-encodings" title="Link to this heading">¶</a></h2>
  1588. <p>A number of predefined codecs are specific to Python, so their codec names have
  1589. no meaning outside Python. These are listed in the tables below based on the
  1590. expected input and output types (note that while text encodings are the most
  1591. common use case for codecs, the underlying codec infrastructure supports
  1592. arbitrary data transforms rather than just text encodings). For asymmetric
  1593. codecs, the stated meaning describes the encoding direction.</p>
  1594. <section id="text-encodings">
  1595. <h3>Text Encodings<a class="headerlink" href="#text-encodings" title="Link to this heading">¶</a></h3>
  1596. <p>The following codecs provide <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> to <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> encoding and
  1597. <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> to <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> decoding, similar to the Unicode text
  1598. encodings.</p>
  1599. <table class="docutils align-default">
  1600. <thead>
  1601. <tr class="row-odd"><th class="head"><p>Codec</p></th>
  1602. <th class="head"><p>Aliases</p></th>
  1603. <th class="head"><p>Meaning</p></th>
  1604. </tr>
  1605. </thead>
  1606. <tbody>
  1607. <tr class="row-even"><td><p>idna</p></td>
  1608. <td></td>
  1609. <td><p>Implement <span class="target" id="index-6"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3490.html"><strong>RFC 3490</strong></a>,
  1610. see also
  1611. <a class="reference internal" href="#module-encodings.idna" title="encodings.idna: Internationalized Domain Names implementation"><code class="xref py py-mod docutils literal notranslate"><span class="pre">encodings.idna</span></code></a>.
  1612. Only <code class="docutils literal notranslate"><span class="pre">errors='strict'</span></code>
  1613. is supported.</p></td>
  1614. </tr>
  1615. <tr class="row-odd"><td><p>mbcs</p></td>
  1616. <td><p>ansi,
  1617. dbcs</p></td>
  1618. <td><p>Windows only: Encode the
  1619. operand according to the
  1620. ANSI codepage (CP_ACP).</p></td>
  1621. </tr>
  1622. <tr class="row-even"><td><p>oem</p></td>
  1623. <td></td>
  1624. <td><p>Windows only: Encode the
  1625. operand according to the
  1626. OEM codepage (CP_OEMCP).</p>
  1627. <div class="versionadded">
  1628. <p><span class="versionmodified added">New in version 3.6.</span></p>
  1629. </div>
  1630. </td>
  1631. </tr>
  1632. <tr class="row-odd"><td><p>palmos</p></td>
  1633. <td></td>
  1634. <td><p>Encoding of PalmOS 3.5.</p></td>
  1635. </tr>
  1636. <tr class="row-even"><td><p>punycode</p></td>
  1637. <td></td>
  1638. <td><p>Implement <span class="target" id="index-7"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3492.html"><strong>RFC 3492</strong></a>.
  1639. Stateful codecs are not
  1640. supported.</p></td>
  1641. </tr>
  1642. <tr class="row-odd"><td><p>raw_unicode_escape</p></td>
  1643. <td></td>
  1644. <td><p>Latin-1 encoding with
  1645. <code class="samp docutils literal notranslate"><span class="pre">\u</span><em><span class="pre">XXXX</span></em></code> and
  1646. <code class="samp docutils literal notranslate"><span class="pre">\U</span><em><span class="pre">XXXXXXXX</span></em></code>
  1647. for other code points.
  1648. Existing
  1649. backslashes are not
  1650. escaped in any way.
  1651. It is used in the Python
  1652. pickle protocol.</p></td>
  1653. </tr>
  1654. <tr class="row-even"><td><p>undefined</p></td>
  1655. <td></td>
  1656. <td><p>Raise an exception for
  1657. all conversions, even
  1658. empty strings. The error
  1659. handler is ignored.</p></td>
  1660. </tr>
  1661. <tr class="row-odd"><td><p>unicode_escape</p></td>
  1662. <td></td>
  1663. <td><p>Encoding suitable as the
  1664. contents of a Unicode
  1665. literal in ASCII-encoded
  1666. Python source code,
  1667. except that quotes are
  1668. not escaped. Decode
  1669. from Latin-1 source code.
  1670. Beware that Python source
  1671. code actually uses UTF-8
  1672. by default.</p></td>
  1673. </tr>
  1674. </tbody>
  1675. </table>
  1676. <div class="versionchanged">
  1677. <p><span class="versionmodified changed">Changed in version 3.8: </span>“unicode_internal” codec is removed.</p>
  1678. </div>
  1679. </section>
  1680. <section id="binary-transforms">
  1681. <span id="id4"></span><h3>Binary Transforms<a class="headerlink" href="#binary-transforms" title="Link to this heading">¶</a></h3>
  1682. <p>The following codecs provide binary transforms: <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>
  1683. to <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> mappings. They are not supported by <a class="reference internal" href="stdtypes.html#bytes.decode" title="bytes.decode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bytes.decode()</span></code></a>
  1684. (which only produces <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> output).</p>
  1685. <table class="docutils align-default">
  1686. <thead>
  1687. <tr class="row-odd"><th class="head"><p>Codec</p></th>
  1688. <th class="head"><p>Aliases</p></th>
  1689. <th class="head"><p>Meaning</p></th>
  1690. <th class="head"><p>Encoder / decoder</p></th>
  1691. </tr>
  1692. </thead>
  1693. <tbody>
  1694. <tr class="row-even"><td><p>base64_codec <a class="footnote-reference brackets" href="#b64" id="id5" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
  1695. <td><p>base64, base_64</p></td>
  1696. <td><p>Convert the operand to
  1697. multiline MIME base64 (the
  1698. result always includes a
  1699. trailing <code class="docutils literal notranslate"><span class="pre">'\n'</span></code>).</p>
  1700. <div class="versionchanged">
  1701. <p><span class="versionmodified changed">Changed in version 3.4: </span>accepts any
  1702. <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>
  1703. as input for encoding and
  1704. decoding</p>
  1705. </div>
  1706. </td>
  1707. <td><p><a class="reference internal" href="base64.html#base64.encodebytes" title="base64.encodebytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">base64.encodebytes()</span></code></a> /
  1708. <a class="reference internal" href="base64.html#base64.decodebytes" title="base64.decodebytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">base64.decodebytes()</span></code></a></p></td>
  1709. </tr>
  1710. <tr class="row-odd"><td><p>bz2_codec</p></td>
  1711. <td><p>bz2</p></td>
  1712. <td><p>Compress the operand using
  1713. bz2.</p></td>
  1714. <td><p><a class="reference internal" href="bz2.html#bz2.compress" title="bz2.compress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bz2.compress()</span></code></a> /
  1715. <a class="reference internal" href="bz2.html#bz2.decompress" title="bz2.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bz2.decompress()</span></code></a></p></td>
  1716. </tr>
  1717. <tr class="row-even"><td><p>hex_codec</p></td>
  1718. <td><p>hex</p></td>
  1719. <td><p>Convert the operand to
  1720. hexadecimal
  1721. representation, with two
  1722. digits per byte.</p></td>
  1723. <td><p><a class="reference internal" href="binascii.html#binascii.b2a_hex" title="binascii.b2a_hex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">binascii.b2a_hex()</span></code></a> /
  1724. <a class="reference internal" href="binascii.html#binascii.a2b_hex" title="binascii.a2b_hex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">binascii.a2b_hex()</span></code></a></p></td>
  1725. </tr>
  1726. <tr class="row-odd"><td><p>quopri_codec</p></td>
  1727. <td><p>quopri,
  1728. quotedprintable,
  1729. quoted_printable</p></td>
  1730. <td><p>Convert the operand to MIME
  1731. quoted printable.</p></td>
  1732. <td><p><a class="reference internal" href="quopri.html#quopri.encode" title="quopri.encode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">quopri.encode()</span></code></a> with
  1733. <code class="docutils literal notranslate"><span class="pre">quotetabs=True</span></code> /
  1734. <a class="reference internal" href="quopri.html#quopri.decode" title="quopri.decode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">quopri.decode()</span></code></a></p></td>
  1735. </tr>
  1736. <tr class="row-even"><td><p>uu_codec</p></td>
  1737. <td><p>uu</p></td>
  1738. <td><p>Convert the operand using
  1739. uuencode.</p></td>
  1740. <td><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">uu.encode()</span></code> /
  1741. <code class="xref py py-meth docutils literal notranslate"><span class="pre">uu.decode()</span></code>
  1742. (Note: <a class="reference internal" href="uu.html#module-uu" title="uu: Encode and decode files in uuencode format. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">uu</span></code></a> is
  1743. deprecated.)</p></td>
  1744. </tr>
  1745. <tr class="row-odd"><td><p>zlib_codec</p></td>
  1746. <td><p>zip, zlib</p></td>
  1747. <td><p>Compress the operand using
  1748. gzip.</p></td>
  1749. <td><p><a class="reference internal" href="zlib.html#zlib.compress" title="zlib.compress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">zlib.compress()</span></code></a> /
  1750. <a class="reference internal" href="zlib.html#zlib.decompress" title="zlib.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">zlib.decompress()</span></code></a></p></td>
  1751. </tr>
  1752. </tbody>
  1753. </table>
  1754. <aside class="footnote-list brackets">
  1755. <aside class="footnote brackets" id="b64" role="doc-footnote">
  1756. <span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id5">1</a><span class="fn-bracket">]</span></span>
  1757. <p>In addition to <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a>,
  1758. <code class="docutils literal notranslate"><span class="pre">'base64_codec'</span></code> also accepts ASCII-only instances of <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> for
  1759. decoding</p>
  1760. </aside>
  1761. </aside>
  1762. <div class="versionadded">
  1763. <p><span class="versionmodified added">New in version 3.2: </span>Restoration of the binary transforms.</p>
  1764. </div>
  1765. <div class="versionchanged">
  1766. <p><span class="versionmodified changed">Changed in version 3.4: </span>Restoration of the aliases for the binary transforms.</p>
  1767. </div>
  1768. </section>
  1769. <section id="text-transforms">
  1770. <span id="id6"></span><h3>Text Transforms<a class="headerlink" href="#text-transforms" title="Link to this heading">¶</a></h3>
  1771. <p>The following codec provides a text transform: a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> to <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>
  1772. mapping. It is not supported by <a class="reference internal" href="stdtypes.html#str.encode" title="str.encode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.encode()</span></code></a> (which only produces
  1773. <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> output).</p>
  1774. <table class="docutils align-default">
  1775. <thead>
  1776. <tr class="row-odd"><th class="head"><p>Codec</p></th>
  1777. <th class="head"><p>Aliases</p></th>
  1778. <th class="head"><p>Meaning</p></th>
  1779. </tr>
  1780. </thead>
  1781. <tbody>
  1782. <tr class="row-even"><td><p>rot_13</p></td>
  1783. <td><p>rot13</p></td>
  1784. <td><p>Return the Caesar-cypher
  1785. encryption of the
  1786. operand.</p></td>
  1787. </tr>
  1788. </tbody>
  1789. </table>
  1790. <div class="versionadded">
  1791. <p><span class="versionmodified added">New in version 3.2: </span>Restoration of the <code class="docutils literal notranslate"><span class="pre">rot_13</span></code> text transform.</p>
  1792. </div>
  1793. <div class="versionchanged">
  1794. <p><span class="versionmodified changed">Changed in version 3.4: </span>Restoration of the <code class="docutils literal notranslate"><span class="pre">rot13</span></code> alias.</p>
  1795. </div>
  1796. </section>
  1797. </section>
  1798. <section id="module-encodings.idna">
  1799. <span id="encodings-idna-internationalized-domain-names-in-applications"></span><h2><a class="reference internal" href="#module-encodings.idna" title="encodings.idna: Internationalized Domain Names implementation"><code class="xref py py-mod docutils literal notranslate"><span class="pre">encodings.idna</span></code></a> — Internationalized Domain Names in Applications<a class="headerlink" href="#module-encodings.idna" title="Link to this heading">¶</a></h2>
  1800. <p>This module implements <span class="target" id="index-8"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3490.html"><strong>RFC 3490</strong></a> (Internationalized Domain Names in
  1801. Applications) and <span class="target" id="index-9"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3492.html"><strong>RFC 3492</strong></a> (Nameprep: A Stringprep Profile for
  1802. Internationalized Domain Names (IDN)). It builds upon the <code class="docutils literal notranslate"><span class="pre">punycode</span></code> encoding
  1803. and <a class="reference internal" href="stringprep.html#module-stringprep" title="stringprep: String preparation, as per RFC 3453"><code class="xref py py-mod docutils literal notranslate"><span class="pre">stringprep</span></code></a>.</p>
  1804. <p>If you need the IDNA 2008 standard from <span class="target" id="index-10"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5891.html"><strong>RFC 5891</strong></a> and <span class="target" id="index-11"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5895.html"><strong>RFC 5895</strong></a>, use the
  1805. third-party <a class="reference external" href="https://pypi.org/project/idna/">idna module</a>.</p>
  1806. <p>These RFCs together define a protocol to support non-ASCII characters in domain
  1807. names. A domain name containing non-ASCII characters (such as
  1808. <code class="docutils literal notranslate"><span class="pre">www.Alliancefrançaise.nu</span></code>) is converted into an ASCII-compatible encoding
  1809. (ACE, such as <code class="docutils literal notranslate"><span class="pre">www.xn--alliancefranaise-npb.nu</span></code>). The ACE form of the domain
  1810. name is then used in all places where arbitrary characters are not allowed by
  1811. the protocol, such as DNS queries, HTTP <em class="mailheader">Host</em> fields, and so
  1812. on. This conversion is carried out in the application; if possible invisible to
  1813. the user: The application should transparently convert Unicode domain labels to
  1814. IDNA on the wire, and convert back ACE labels to Unicode before presenting them
  1815. to the user.</p>
  1816. <p>Python supports this conversion in several ways: the <code class="docutils literal notranslate"><span class="pre">idna</span></code> codec performs
  1817. conversion between Unicode and ACE, separating an input string into labels
  1818. based on the separator characters defined in <span class="target" id="index-12"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3490.html#section-3.1"><strong>section 3.1 of RFC 3490</strong></a>
  1819. and converting each label to ACE as required, and conversely separating an input
  1820. byte string into labels based on the <code class="docutils literal notranslate"><span class="pre">.</span></code> separator and converting any ACE
  1821. labels found into unicode. Furthermore, the <a class="reference internal" href="socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a> module
  1822. transparently converts Unicode host names to ACE, so that applications need not
  1823. be concerned about converting host names themselves when they pass them to the
  1824. socket module. On top of that, modules that have host names as function
  1825. parameters, such as <a class="reference internal" href="http.client.html#module-http.client" title="http.client: HTTP and HTTPS protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.client</span></code></a> and <a class="reference internal" href="ftplib.html#module-ftplib" title="ftplib: FTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ftplib</span></code></a>, accept Unicode host
  1826. names (<a class="reference internal" href="http.client.html#module-http.client" title="http.client: HTTP and HTTPS protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.client</span></code></a> then also transparently sends an IDNA hostname in the
  1827. <em class="mailheader">Host</em> field if it sends that field at all).</p>
  1828. <p>When receiving host names from the wire (such as in reverse name lookup), no
  1829. automatic conversion to Unicode is performed: applications wishing to present
  1830. such host names to the user should decode them to Unicode.</p>
  1831. <p>The module <a class="reference internal" href="#module-encodings.idna" title="encodings.idna: Internationalized Domain Names implementation"><code class="xref py py-mod docutils literal notranslate"><span class="pre">encodings.idna</span></code></a> also implements the nameprep procedure, which
  1832. performs certain normalizations on host names, to achieve case-insensitivity of
  1833. international domain names, and to unify similar characters. The nameprep
  1834. functions can be used directly if desired.</p>
  1835. <dl class="py function">
  1836. <dt class="sig sig-object py" id="encodings.idna.nameprep">
  1837. <span class="sig-prename descclassname"><span class="pre">encodings.idna.</span></span><span class="sig-name descname"><span class="pre">nameprep</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">label</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#encodings.idna.nameprep" title="Link to this definition">¶</a></dt>
  1838. <dd><p>Return the nameprepped version of <em>label</em>. The implementation currently assumes
  1839. query strings, so <code class="docutils literal notranslate"><span class="pre">AllowUnassigned</span></code> is true.</p>
  1840. </dd></dl>
  1841. <dl class="py function">
  1842. <dt class="sig sig-object py" id="encodings.idna.ToASCII">
  1843. <span class="sig-prename descclassname"><span class="pre">encodings.idna.</span></span><span class="sig-name descname"><span class="pre">ToASCII</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">label</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#encodings.idna.ToASCII" title="Link to this definition">¶</a></dt>
  1844. <dd><p>Convert a label to ASCII, as specified in <span class="target" id="index-13"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3490.html"><strong>RFC 3490</strong></a>. <code class="docutils literal notranslate"><span class="pre">UseSTD3ASCIIRules</span></code> is
  1845. assumed to be false.</p>
  1846. </dd></dl>
  1847. <dl class="py function">
  1848. <dt class="sig sig-object py" id="encodings.idna.ToUnicode">
  1849. <span class="sig-prename descclassname"><span class="pre">encodings.idna.</span></span><span class="sig-name descname"><span class="pre">ToUnicode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">label</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#encodings.idna.ToUnicode" title="Link to this definition">¶</a></dt>
  1850. <dd><p>Convert a label to Unicode, as specified in <span class="target" id="index-14"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3490.html"><strong>RFC 3490</strong></a>.</p>
  1851. </dd></dl>
  1852. </section>
  1853. <section id="module-encodings.mbcs">
  1854. <span id="encodings-mbcs-windows-ansi-codepage"></span><h2><a class="reference internal" href="#module-encodings.mbcs" title="encodings.mbcs: Windows ANSI codepage"><code class="xref py py-mod docutils literal notranslate"><span class="pre">encodings.mbcs</span></code></a> — Windows ANSI codepage<a class="headerlink" href="#module-encodings.mbcs" title="Link to this heading">¶</a></h2>
  1855. <p>This module implements the ANSI codepage (CP_ACP).</p>
  1856. <div class="availability docutils container">
  1857. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p>
  1858. </div>
  1859. <div class="versionchanged">
  1860. <p><span class="versionmodified changed">Changed in version 3.2: </span>Before 3.2, the <em>errors</em> argument was ignored; <code class="docutils literal notranslate"><span class="pre">'replace'</span></code> was always used
  1861. to encode, and <code class="docutils literal notranslate"><span class="pre">'ignore'</span></code> to decode.</p>
  1862. </div>
  1863. <div class="versionchanged">
  1864. <p><span class="versionmodified changed">Changed in version 3.3: </span>Support any error handler.</p>
  1865. </div>
  1866. </section>
  1867. <section id="module-encodings.utf_8_sig">
  1868. <span id="encodings-utf-8-sig-utf-8-codec-with-bom-signature"></span><h2><a class="reference internal" href="#module-encodings.utf_8_sig" title="encodings.utf_8_sig: UTF-8 codec with BOM signature"><code class="xref py py-mod docutils literal notranslate"><span class="pre">encodings.utf_8_sig</span></code></a> — UTF-8 codec with BOM signature<a class="headerlink" href="#module-encodings.utf_8_sig" title="Link to this heading">¶</a></h2>
  1869. <p>This module implements a variant of the UTF-8 codec. On encoding, a UTF-8 encoded
  1870. BOM will be prepended to the UTF-8 encoded bytes. For the stateful encoder this
  1871. is only done once (on the first write to the byte stream). On decoding, an
  1872. optional UTF-8 encoded BOM at the start of the data will be skipped.</p>
  1873. </section>
  1874. </section>
  1875. <div class="clearer"></div>
  1876. </div>
  1877. </div>
  1878. </div>
  1879. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1880. <div class="sphinxsidebarwrapper">
  1881. <div>
  1882. <h3><a href="../contents.html">Table of Contents</a></h3>
  1883. <ul>
  1884. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">codecs</span></code> — Codec registry and base classes</a><ul>
  1885. <li><a class="reference internal" href="#codec-base-classes">Codec Base Classes</a><ul>
  1886. <li><a class="reference internal" href="#error-handlers">Error Handlers</a></li>
  1887. <li><a class="reference internal" href="#stateless-encoding-and-decoding">Stateless Encoding and Decoding</a></li>
  1888. <li><a class="reference internal" href="#incremental-encoding-and-decoding">Incremental Encoding and Decoding</a><ul>
  1889. <li><a class="reference internal" href="#incrementalencoder-objects">IncrementalEncoder Objects</a></li>
  1890. <li><a class="reference internal" href="#incrementaldecoder-objects">IncrementalDecoder Objects</a></li>
  1891. </ul>
  1892. </li>
  1893. <li><a class="reference internal" href="#stream-encoding-and-decoding">Stream Encoding and Decoding</a><ul>
  1894. <li><a class="reference internal" href="#streamwriter-objects">StreamWriter Objects</a></li>
  1895. <li><a class="reference internal" href="#streamreader-objects">StreamReader Objects</a></li>
  1896. <li><a class="reference internal" href="#streamreaderwriter-objects">StreamReaderWriter Objects</a></li>
  1897. <li><a class="reference internal" href="#streamrecoder-objects">StreamRecoder Objects</a></li>
  1898. </ul>
  1899. </li>
  1900. </ul>
  1901. </li>
  1902. <li><a class="reference internal" href="#encodings-and-unicode">Encodings and Unicode</a></li>
  1903. <li><a class="reference internal" href="#standard-encodings">Standard Encodings</a></li>
  1904. <li><a class="reference internal" href="#python-specific-encodings">Python Specific Encodings</a><ul>
  1905. <li><a class="reference internal" href="#text-encodings">Text Encodings</a></li>
  1906. <li><a class="reference internal" href="#binary-transforms">Binary Transforms</a></li>
  1907. <li><a class="reference internal" href="#text-transforms">Text Transforms</a></li>
  1908. </ul>
  1909. </li>
  1910. <li><a class="reference internal" href="#module-encodings.idna"><code class="xref py py-mod docutils literal notranslate"><span class="pre">encodings.idna</span></code> — Internationalized Domain Names in Applications</a></li>
  1911. <li><a class="reference internal" href="#module-encodings.mbcs"><code class="xref py py-mod docutils literal notranslate"><span class="pre">encodings.mbcs</span></code> — Windows ANSI codepage</a></li>
  1912. <li><a class="reference internal" href="#module-encodings.utf_8_sig"><code class="xref py py-mod docutils literal notranslate"><span class="pre">encodings.utf_8_sig</span></code> — UTF-8 codec with BOM signature</a></li>
  1913. </ul>
  1914. </li>
  1915. </ul>
  1916. </div>
  1917. <div>
  1918. <h4>Previous topic</h4>
  1919. <p class="topless"><a href="struct.html"
  1920. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code> — Interpret bytes as packed binary data</a></p>
  1921. </div>
  1922. <div>
  1923. <h4>Next topic</h4>
  1924. <p class="topless"><a href="datatypes.html"
  1925. title="next chapter">Data Types</a></p>
  1926. </div>
  1927. <div role="note" aria-label="source link">
  1928. <h3>This Page</h3>
  1929. <ul class="this-page-menu">
  1930. <li><a href="../bugs.html">Report a Bug</a></li>
  1931. <li>
  1932. <a href="https://github.com/python/cpython/blob/main/Doc/library/codecs.rst"
  1933. rel="nofollow">Show Source
  1934. </a>
  1935. </li>
  1936. </ul>
  1937. </div>
  1938. </div>
  1939. <div id="sidebarbutton" title="Collapse sidebar">
  1940. <span>«</span>
  1941. </div>
  1942. </div>
  1943. <div class="clearer"></div>
  1944. </div>
  1945. <div class="related" role="navigation" aria-label="related navigation">
  1946. <h3>Navigation</h3>
  1947. <ul>
  1948. <li class="right" style="margin-right: 10px">
  1949. <a href="../genindex.html" title="General Index"
  1950. >index</a></li>
  1951. <li class="right" >
  1952. <a href="../py-modindex.html" title="Python Module Index"
  1953. >modules</a> |</li>
  1954. <li class="right" >
  1955. <a href="datatypes.html" title="Data Types"
  1956. >next</a> |</li>
  1957. <li class="right" >
  1958. <a href="struct.html" title="struct — Interpret bytes as packed binary data"
  1959. >previous</a> |</li>
  1960. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1961. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1962. <li class="switchers">
  1963. <div class="language_switcher_placeholder"></div>
  1964. <div class="version_switcher_placeholder"></div>
  1965. </li>
  1966. <li>
  1967. </li>
  1968. <li id="cpython-language-and-version">
  1969. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1970. </li>
  1971. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1972. <li class="nav-item nav-item-2"><a href="binary.html" >Binary Data Services</a> &#187;</li>
  1973. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">codecs</span></code> — Codec registry and base classes</a></li>
  1974. <li class="right">
  1975. <div class="inline-search" role="search">
  1976. <form class="inline-search" action="../search.html" method="get">
  1977. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1978. <input type="submit" value="Go" />
  1979. </form>
  1980. </div>
  1981. |
  1982. </li>
  1983. <li class="right">
  1984. <label class="theme-selector-label">
  1985. Theme
  1986. <select class="theme-selector" oninput="activateTheme(this.value)">
  1987. <option value="auto" selected>Auto</option>
  1988. <option value="light">Light</option>
  1989. <option value="dark">Dark</option>
  1990. </select>
  1991. </label> |</li>
  1992. </ul>
  1993. </div>
  1994. <div class="footer">
  1995. &copy;
  1996. <a href="../copyright.html">
  1997. Copyright
  1998. </a>
  1999. 2001-2024, Python Software Foundation.
  2000. <br />
  2001. This page is licensed under the Python Software Foundation License Version 2.
  2002. <br />
  2003. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  2004. <br />
  2005. See <a href="/license.html">History and License</a> for more information.<br />
  2006. <br />
  2007. The Python Software Foundation is a non-profit corporation.
  2008. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  2009. <br />
  2010. <br />
  2011. Last updated on Apr 09, 2024 (13:47 UTC).
  2012. <a href="/bugs.html">Found a bug</a>?
  2013. <br />
  2014. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  2015. </div>
  2016. </body>
  2017. </html>
上海开阖软件有限公司 沪ICP备12045867号-1