gooderp18绿色标准版
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

513 rindas
34KB

  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="email.charset: Representing character sets" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/email.charset.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/email/charset.py This module is part of the legacy ( Compat32) email API. In the new API only the aliases table is used. The remaining text in this section is the original document..." />
  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/email/charset.py This module is part of the legacy ( Compat32) email API. In the new API only the aliases table is used. The remaining text in this section is the original document..." />
  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>email.charset: Representing character sets &#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="email.encoders: Encoders" href="email.encoders.html" />
  33. <link rel="prev" title="email.header: Internationalized headers" href="email.header.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/email.charset.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. <h4>Previous topic</h4>
  84. <p class="topless"><a href="email.header.html"
  85. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.header</span></code>: Internationalized headers</a></p>
  86. </div>
  87. <div>
  88. <h4>Next topic</h4>
  89. <p class="topless"><a href="email.encoders.html"
  90. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.encoders</span></code>: Encoders</a></p>
  91. </div>
  92. <div role="note" aria-label="source link">
  93. <h3>This Page</h3>
  94. <ul class="this-page-menu">
  95. <li><a href="../bugs.html">Report a Bug</a></li>
  96. <li>
  97. <a href="https://github.com/python/cpython/blob/main/Doc/library/email.charset.rst"
  98. rel="nofollow">Show Source
  99. </a>
  100. </li>
  101. </ul>
  102. </div>
  103. </nav>
  104. </div>
  105. </div>
  106. <div class="related" role="navigation" aria-label="related navigation">
  107. <h3>Navigation</h3>
  108. <ul>
  109. <li class="right" style="margin-right: 10px">
  110. <a href="../genindex.html" title="General Index"
  111. accesskey="I">index</a></li>
  112. <li class="right" >
  113. <a href="../py-modindex.html" title="Python Module Index"
  114. >modules</a> |</li>
  115. <li class="right" >
  116. <a href="email.encoders.html" title="email.encoders: Encoders"
  117. accesskey="N">next</a> |</li>
  118. <li class="right" >
  119. <a href="email.header.html" title="email.header: Internationalized headers"
  120. accesskey="P">previous</a> |</li>
  121. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  122. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  123. <li class="switchers">
  124. <div class="language_switcher_placeholder"></div>
  125. <div class="version_switcher_placeholder"></div>
  126. </li>
  127. <li>
  128. </li>
  129. <li id="cpython-language-and-version">
  130. <a href="../index.html">3.12.3 Documentation</a> &#187;
  131. </li>
  132. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  133. <li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> &#187;</li>
  134. <li class="nav-item nav-item-3"><a href="email.html" accesskey="U"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code> — An email and MIME handling package</a> &#187;</li>
  135. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.charset</span></code>: Representing character sets</a></li>
  136. <li class="right">
  137. <div class="inline-search" role="search">
  138. <form class="inline-search" action="../search.html" method="get">
  139. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  140. <input type="submit" value="Go" />
  141. </form>
  142. </div>
  143. |
  144. </li>
  145. <li class="right">
  146. <label class="theme-selector-label">
  147. Theme
  148. <select class="theme-selector" oninput="activateTheme(this.value)">
  149. <option value="auto" selected>Auto</option>
  150. <option value="light">Light</option>
  151. <option value="dark">Dark</option>
  152. </select>
  153. </label> |</li>
  154. </ul>
  155. </div>
  156. <div class="document">
  157. <div class="documentwrapper">
  158. <div class="bodywrapper">
  159. <div class="body" role="main">
  160. <section id="module-email.charset">
  161. <span id="email-charset-representing-character-sets"></span><h1><a class="reference internal" href="#module-email.charset" title="email.charset: Character Sets"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.charset</span></code></a>: Representing character sets<a class="headerlink" href="#module-email.charset" title="Link to this heading">¶</a></h1>
  162. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/email/charset.py">Lib/email/charset.py</a></p>
  163. <hr class="docutils" />
  164. <p>This module is part of the legacy (<code class="docutils literal notranslate"><span class="pre">Compat32</span></code>) email API. In the new
  165. API only the aliases table is used.</p>
  166. <p>The remaining text in this section is the original documentation of the module.</p>
  167. <p>This module provides a class <a class="reference internal" href="#email.charset.Charset" title="email.charset.Charset"><code class="xref py py-class docutils literal notranslate"><span class="pre">Charset</span></code></a> for representing character sets
  168. and character set conversions in email messages, as well as a character set
  169. registry and several convenience methods for manipulating this registry.
  170. Instances of <a class="reference internal" href="#email.charset.Charset" title="email.charset.Charset"><code class="xref py py-class docutils literal notranslate"><span class="pre">Charset</span></code></a> are used in several other modules within the
  171. <a class="reference internal" href="email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code></a> package.</p>
  172. <p>Import this class from the <a class="reference internal" href="#module-email.charset" title="email.charset: Character Sets"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.charset</span></code></a> module.</p>
  173. <dl class="py class">
  174. <dt class="sig sig-object py" id="email.charset.Charset">
  175. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.charset.</span></span><span class="sig-name descname"><span class="pre">Charset</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">input_charset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">DEFAULT_CHARSET</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.charset.Charset" title="Link to this definition">¶</a></dt>
  176. <dd><p>Map character sets to their email properties.</p>
  177. <p>This class provides information about the requirements imposed on email for a
  178. specific character set. It also provides convenience routines for converting
  179. between character sets, given the availability of the applicable codecs. Given
  180. a character set, it will do its best to provide information on how to use that
  181. character set in an email message in an RFC-compliant way.</p>
  182. <p>Certain character sets must be encoded with quoted-printable or base64 when used
  183. in email headers or bodies. Certain character sets must be converted outright,
  184. and are not allowed in email.</p>
  185. <p>Optional <em>input_charset</em> is as described below; it is always coerced to lower
  186. case. After being alias normalized it is also used as a lookup into the
  187. registry of character sets to find out the header encoding, body encoding, and
  188. output conversion codec to be used for the character set. For example, if
  189. <em>input_charset</em> is <code class="docutils literal notranslate"><span class="pre">iso-8859-1</span></code>, then headers and bodies will be encoded using
  190. quoted-printable and no output conversion codec is necessary. If
  191. <em>input_charset</em> is <code class="docutils literal notranslate"><span class="pre">euc-jp</span></code>, then headers will be encoded with base64, bodies
  192. will not be encoded, but output text will be converted from the <code class="docutils literal notranslate"><span class="pre">euc-jp</span></code>
  193. character set to the <code class="docutils literal notranslate"><span class="pre">iso-2022-jp</span></code> character set.</p>
  194. <p><a class="reference internal" href="#email.charset.Charset" title="email.charset.Charset"><code class="xref py py-class docutils literal notranslate"><span class="pre">Charset</span></code></a> instances have the following data attributes:</p>
  195. <dl class="py attribute">
  196. <dt class="sig sig-object py" id="email.charset.Charset.input_charset">
  197. <span class="sig-name descname"><span class="pre">input_charset</span></span><a class="headerlink" href="#email.charset.Charset.input_charset" title="Link to this definition">¶</a></dt>
  198. <dd><p>The initial character set specified. Common aliases are converted to
  199. their <em>official</em> email names (e.g. <code class="docutils literal notranslate"><span class="pre">latin_1</span></code> is converted to
  200. <code class="docutils literal notranslate"><span class="pre">iso-8859-1</span></code>). Defaults to 7-bit <code class="docutils literal notranslate"><span class="pre">us-ascii</span></code>.</p>
  201. </dd></dl>
  202. <dl class="py attribute">
  203. <dt class="sig sig-object py" id="email.charset.Charset.header_encoding">
  204. <span class="sig-name descname"><span class="pre">header_encoding</span></span><a class="headerlink" href="#email.charset.Charset.header_encoding" title="Link to this definition">¶</a></dt>
  205. <dd><p>If the character set must be encoded before it can be used in an email
  206. header, this attribute will be set to <code class="docutils literal notranslate"><span class="pre">charset.QP</span></code> (for
  207. quoted-printable), <code class="docutils literal notranslate"><span class="pre">charset.BASE64</span></code> (for base64 encoding), or
  208. <code class="docutils literal notranslate"><span class="pre">charset.SHORTEST</span></code> for the shortest of QP or BASE64 encoding. Otherwise,
  209. it will be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  210. </dd></dl>
  211. <dl class="py attribute">
  212. <dt class="sig sig-object py" id="email.charset.Charset.body_encoding">
  213. <span class="sig-name descname"><span class="pre">body_encoding</span></span><a class="headerlink" href="#email.charset.Charset.body_encoding" title="Link to this definition">¶</a></dt>
  214. <dd><p>Same as <em>header_encoding</em>, but describes the encoding for the mail
  215. message’s body, which indeed may be different than the header encoding.
  216. <code class="docutils literal notranslate"><span class="pre">charset.SHORTEST</span></code> is not allowed for <em>body_encoding</em>.</p>
  217. </dd></dl>
  218. <dl class="py attribute">
  219. <dt class="sig sig-object py" id="email.charset.Charset.output_charset">
  220. <span class="sig-name descname"><span class="pre">output_charset</span></span><a class="headerlink" href="#email.charset.Charset.output_charset" title="Link to this definition">¶</a></dt>
  221. <dd><p>Some character sets must be converted before they can be used in email
  222. headers or bodies. If the <em>input_charset</em> is one of them, this attribute
  223. will contain the name of the character set output will be converted to.
  224. Otherwise, it will be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  225. </dd></dl>
  226. <dl class="py attribute">
  227. <dt class="sig sig-object py" id="email.charset.Charset.input_codec">
  228. <span class="sig-name descname"><span class="pre">input_codec</span></span><a class="headerlink" href="#email.charset.Charset.input_codec" title="Link to this definition">¶</a></dt>
  229. <dd><p>The name of the Python codec used to convert the <em>input_charset</em> to
  230. Unicode. If no conversion codec is necessary, this attribute will be
  231. <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  232. </dd></dl>
  233. <dl class="py attribute">
  234. <dt class="sig sig-object py" id="email.charset.Charset.output_codec">
  235. <span class="sig-name descname"><span class="pre">output_codec</span></span><a class="headerlink" href="#email.charset.Charset.output_codec" title="Link to this definition">¶</a></dt>
  236. <dd><p>The name of the Python codec used to convert Unicode to the
  237. <em>output_charset</em>. If no conversion codec is necessary, this attribute
  238. will have the same value as the <em>input_codec</em>.</p>
  239. </dd></dl>
  240. <p><a class="reference internal" href="#email.charset.Charset" title="email.charset.Charset"><code class="xref py py-class docutils literal notranslate"><span class="pre">Charset</span></code></a> instances also have the following methods:</p>
  241. <dl class="py method">
  242. <dt class="sig sig-object py" id="email.charset.Charset.get_body_encoding">
  243. <span class="sig-name descname"><span class="pre">get_body_encoding</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#email.charset.Charset.get_body_encoding" title="Link to this definition">¶</a></dt>
  244. <dd><p>Return the content transfer encoding used for body encoding.</p>
  245. <p>This is either the string <code class="docutils literal notranslate"><span class="pre">quoted-printable</span></code> or <code class="docutils literal notranslate"><span class="pre">base64</span></code> depending on
  246. the encoding used, or it is a function, in which case you should call the
  247. function with a single argument, the Message object being encoded. The
  248. function should then set the <em class="mailheader">Content-Transfer-Encoding</em>
  249. header itself to whatever is appropriate.</p>
  250. <p>Returns the string <code class="docutils literal notranslate"><span class="pre">quoted-printable</span></code> if <em>body_encoding</em> is <code class="docutils literal notranslate"><span class="pre">QP</span></code>,
  251. returns the string <code class="docutils literal notranslate"><span class="pre">base64</span></code> if <em>body_encoding</em> is <code class="docutils literal notranslate"><span class="pre">BASE64</span></code>, and
  252. returns the string <code class="docutils literal notranslate"><span class="pre">7bit</span></code> otherwise.</p>
  253. </dd></dl>
  254. <dl class="py method">
  255. <dt class="sig sig-object py" id="email.charset.Charset.get_output_charset">
  256. <span class="sig-name descname"><span class="pre">get_output_charset</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#email.charset.Charset.get_output_charset" title="Link to this definition">¶</a></dt>
  257. <dd><p>Return the output character set.</p>
  258. <p>This is the <em>output_charset</em> attribute if that is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, otherwise
  259. it is <em>input_charset</em>.</p>
  260. </dd></dl>
  261. <dl class="py method">
  262. <dt class="sig sig-object py" id="email.charset.Charset.header_encode">
  263. <span class="sig-name descname"><span class="pre">header_encode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.charset.Charset.header_encode" title="Link to this definition">¶</a></dt>
  264. <dd><p>Header-encode the string <em>string</em>.</p>
  265. <p>The type of encoding (base64 or quoted-printable) will be based on the
  266. <em>header_encoding</em> attribute.</p>
  267. </dd></dl>
  268. <dl class="py method">
  269. <dt class="sig sig-object py" id="email.charset.Charset.header_encode_lines">
  270. <span class="sig-name descname"><span class="pre">header_encode_lines</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">maxlengths</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.charset.Charset.header_encode_lines" title="Link to this definition">¶</a></dt>
  271. <dd><p>Header-encode a <em>string</em> by converting it first to bytes.</p>
  272. <p>This is similar to <a class="reference internal" href="#email.charset.Charset.header_encode" title="email.charset.Charset.header_encode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">header_encode()</span></code></a> except that the string is fit
  273. into maximum line lengths as given by the argument <em>maxlengths</em>, which
  274. must be an iterator: each element returned from this iterator will provide
  275. the next maximum line length.</p>
  276. </dd></dl>
  277. <dl class="py method">
  278. <dt class="sig sig-object py" id="email.charset.Charset.body_encode">
  279. <span class="sig-name descname"><span class="pre">body_encode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">string</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.charset.Charset.body_encode" title="Link to this definition">¶</a></dt>
  280. <dd><p>Body-encode the string <em>string</em>.</p>
  281. <p>The type of encoding (base64 or quoted-printable) will be based on the
  282. <em>body_encoding</em> attribute.</p>
  283. </dd></dl>
  284. <p>The <a class="reference internal" href="#email.charset.Charset" title="email.charset.Charset"><code class="xref py py-class docutils literal notranslate"><span class="pre">Charset</span></code></a> class also provides a number of methods to support
  285. standard operations and built-in functions.</p>
  286. <dl class="py method">
  287. <dt class="sig sig-object py" id="email.charset.Charset.__str__">
  288. <span class="sig-name descname"><span class="pre">__str__</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#email.charset.Charset.__str__" title="Link to this definition">¶</a></dt>
  289. <dd><p>Returns <em>input_charset</em> as a string coerced to lower
  290. case. <code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code> is an alias for <code class="xref py py-meth docutils literal notranslate"><span class="pre">__str__()</span></code>.</p>
  291. </dd></dl>
  292. <dl class="py method">
  293. <dt class="sig sig-object py" id="email.charset.Charset.__eq__">
  294. <span class="sig-name descname"><span class="pre">__eq__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">other</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.charset.Charset.__eq__" title="Link to this definition">¶</a></dt>
  295. <dd><p>This method allows you to compare two <a class="reference internal" href="#email.charset.Charset" title="email.charset.Charset"><code class="xref py py-class docutils literal notranslate"><span class="pre">Charset</span></code></a> instances for
  296. equality.</p>
  297. </dd></dl>
  298. <dl class="py method">
  299. <dt class="sig sig-object py" id="email.charset.Charset.__ne__">
  300. <span class="sig-name descname"><span class="pre">__ne__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">other</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.charset.Charset.__ne__" title="Link to this definition">¶</a></dt>
  301. <dd><p>This method allows you to compare two <a class="reference internal" href="#email.charset.Charset" title="email.charset.Charset"><code class="xref py py-class docutils literal notranslate"><span class="pre">Charset</span></code></a> instances for
  302. inequality.</p>
  303. </dd></dl>
  304. </dd></dl>
  305. <p>The <a class="reference internal" href="#module-email.charset" title="email.charset: Character Sets"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.charset</span></code></a> module also provides the following functions for adding
  306. new entries to the global character set, alias, and codec registries:</p>
  307. <dl class="py function">
  308. <dt class="sig sig-object py" id="email.charset.add_charset">
  309. <span class="sig-prename descclassname"><span class="pre">email.charset.</span></span><span class="sig-name descname"><span class="pre">add_charset</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">charset</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">header_enc</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">body_enc</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">output_charset</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="#email.charset.add_charset" title="Link to this definition">¶</a></dt>
  310. <dd><p>Add character properties to the global registry.</p>
  311. <p><em>charset</em> is the input character set, and must be the canonical name of a
  312. character set.</p>
  313. <p>Optional <em>header_enc</em> and <em>body_enc</em> is either <code class="docutils literal notranslate"><span class="pre">charset.QP</span></code> for
  314. quoted-printable, <code class="docutils literal notranslate"><span class="pre">charset.BASE64</span></code> for base64 encoding,
  315. <code class="docutils literal notranslate"><span class="pre">charset.SHORTEST</span></code> for the shortest of quoted-printable or base64 encoding,
  316. or <code class="docutils literal notranslate"><span class="pre">None</span></code> for no encoding. <code class="docutils literal notranslate"><span class="pre">SHORTEST</span></code> is only valid for
  317. <em>header_enc</em>. The default is <code class="docutils literal notranslate"><span class="pre">None</span></code> for no encoding.</p>
  318. <p>Optional <em>output_charset</em> is the character set that the output should be in.
  319. Conversions will proceed from input charset, to Unicode, to the output charset
  320. when the method <code class="xref py py-meth docutils literal notranslate"><span class="pre">Charset.convert()</span></code> is called. The default is to output in
  321. the same character set as the input.</p>
  322. <p>Both <em>input_charset</em> and <em>output_charset</em> must have Unicode codec entries in the
  323. module’s character set-to-codec mapping; use <a class="reference internal" href="#email.charset.add_codec" title="email.charset.add_codec"><code class="xref py py-func docutils literal notranslate"><span class="pre">add_codec()</span></code></a> to add codecs the
  324. module does not know about. See the <a class="reference internal" href="codecs.html#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’s documentation for
  325. more information.</p>
  326. <p>The global character set registry is kept in the module global dictionary
  327. <code class="docutils literal notranslate"><span class="pre">CHARSETS</span></code>.</p>
  328. </dd></dl>
  329. <dl class="py function">
  330. <dt class="sig sig-object py" id="email.charset.add_alias">
  331. <span class="sig-prename descclassname"><span class="pre">email.charset.</span></span><span class="sig-name descname"><span class="pre">add_alias</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">alias</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">canonical</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.charset.add_alias" title="Link to this definition">¶</a></dt>
  332. <dd><p>Add a character set alias. <em>alias</em> is the alias name, e.g. <code class="docutils literal notranslate"><span class="pre">latin-1</span></code>.
  333. <em>canonical</em> is the character set’s canonical name, e.g. <code class="docutils literal notranslate"><span class="pre">iso-8859-1</span></code>.</p>
  334. <p>The global charset alias registry is kept in the module global dictionary
  335. <code class="docutils literal notranslate"><span class="pre">ALIASES</span></code>.</p>
  336. </dd></dl>
  337. <dl class="py function">
  338. <dt class="sig sig-object py" id="email.charset.add_codec">
  339. <span class="sig-prename descclassname"><span class="pre">email.charset.</span></span><span class="sig-name descname"><span class="pre">add_codec</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">charset</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">codecname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.charset.add_codec" title="Link to this definition">¶</a></dt>
  340. <dd><p>Add a codec that map characters in the given character set to and from Unicode.</p>
  341. <p><em>charset</em> is the canonical name of a character set. <em>codecname</em> is the name of a
  342. Python codec, as appropriate for the second argument to the <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>’s
  343. <a class="reference internal" href="stdtypes.html#str.encode" title="str.encode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">encode()</span></code></a> method.</p>
  344. </dd></dl>
  345. </section>
  346. <div class="clearer"></div>
  347. </div>
  348. </div>
  349. </div>
  350. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  351. <div class="sphinxsidebarwrapper">
  352. <div>
  353. <h4>Previous topic</h4>
  354. <p class="topless"><a href="email.header.html"
  355. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.header</span></code>: Internationalized headers</a></p>
  356. </div>
  357. <div>
  358. <h4>Next topic</h4>
  359. <p class="topless"><a href="email.encoders.html"
  360. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.encoders</span></code>: Encoders</a></p>
  361. </div>
  362. <div role="note" aria-label="source link">
  363. <h3>This Page</h3>
  364. <ul class="this-page-menu">
  365. <li><a href="../bugs.html">Report a Bug</a></li>
  366. <li>
  367. <a href="https://github.com/python/cpython/blob/main/Doc/library/email.charset.rst"
  368. rel="nofollow">Show Source
  369. </a>
  370. </li>
  371. </ul>
  372. </div>
  373. </div>
  374. <div id="sidebarbutton" title="Collapse sidebar">
  375. <span>«</span>
  376. </div>
  377. </div>
  378. <div class="clearer"></div>
  379. </div>
  380. <div class="related" role="navigation" aria-label="related navigation">
  381. <h3>Navigation</h3>
  382. <ul>
  383. <li class="right" style="margin-right: 10px">
  384. <a href="../genindex.html" title="General Index"
  385. >index</a></li>
  386. <li class="right" >
  387. <a href="../py-modindex.html" title="Python Module Index"
  388. >modules</a> |</li>
  389. <li class="right" >
  390. <a href="email.encoders.html" title="email.encoders: Encoders"
  391. >next</a> |</li>
  392. <li class="right" >
  393. <a href="email.header.html" title="email.header: Internationalized headers"
  394. >previous</a> |</li>
  395. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  396. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  397. <li class="switchers">
  398. <div class="language_switcher_placeholder"></div>
  399. <div class="version_switcher_placeholder"></div>
  400. </li>
  401. <li>
  402. </li>
  403. <li id="cpython-language-and-version">
  404. <a href="../index.html">3.12.3 Documentation</a> &#187;
  405. </li>
  406. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  407. <li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> &#187;</li>
  408. <li class="nav-item nav-item-3"><a href="email.html" ><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code> — An email and MIME handling package</a> &#187;</li>
  409. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.charset</span></code>: Representing character sets</a></li>
  410. <li class="right">
  411. <div class="inline-search" role="search">
  412. <form class="inline-search" action="../search.html" method="get">
  413. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  414. <input type="submit" value="Go" />
  415. </form>
  416. </div>
  417. |
  418. </li>
  419. <li class="right">
  420. <label class="theme-selector-label">
  421. Theme
  422. <select class="theme-selector" oninput="activateTheme(this.value)">
  423. <option value="auto" selected>Auto</option>
  424. <option value="light">Light</option>
  425. <option value="dark">Dark</option>
  426. </select>
  427. </label> |</li>
  428. </ul>
  429. </div>
  430. <div class="footer">
  431. &copy;
  432. <a href="../copyright.html">
  433. Copyright
  434. </a>
  435. 2001-2024, Python Software Foundation.
  436. <br />
  437. This page is licensed under the Python Software Foundation License Version 2.
  438. <br />
  439. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  440. <br />
  441. See <a href="/license.html">History and License</a> for more information.<br />
  442. <br />
  443. The Python Software Foundation is a non-profit corporation.
  444. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  445. <br />
  446. <br />
  447. Last updated on Apr 09, 2024 (13:47 UTC).
  448. <a href="/bugs.html">Found a bug</a>?
  449. <br />
  450. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  451. </div>
  452. </body>
  453. </html>
上海开阖软件有限公司 沪ICP备12045867号-1