Python: gb2312 codec can't decode bytes
<p>I have a word-encoded string from received mail. When parsing encoded word in Python3, I got an exception </p>

<blockquote>
  <p>'gb2312' codec can't decode bytes in position 18-19: illegal multibyte
  sequence</p>
</blockquote>

<p>raised from <em>make_header</em> method.</p>

<pre><code>from email.header import decode_header, make_header

hdr = decode_header("""=?gb2312?B?QSBWIM34IMXMILP2IMrbICAgqEMgs8kgyMsg?=""")
make_header(hdr)
</code></pre>

<p>Parsing encoded string in online tools works without problems (<a href="http://dogmamix.com/MimeHeadersDecoder/" rel="nofollow">http://dogmamix.com/MimeHeadersDecoder/</a>).
Any suggestions what I am doing wrong? Thanks</p>

<p>The error message tells you that the bytes <em>in position 18-19</em> are not valid for this encoding.</p>

<p><code>decode_header</code> simply extracts a bunch of bytes and an encoding.  <code>make_header</code> actually attempts to interpret those bytes in that encoding, and fails, because these bytes are not valid in that encoding.</p>

<p>Similarly,</p>

<pre><code>bash$ base64 -D &lt;&lt;&lt;'QSBWIM34IMXMILP2IMrbICAgqEMgs8kgyMsg' |
&gt; iconv -f gb2312 -t utf-8
A V ç½ ç åº å®   
iconv: (stdin):1:18: cannot convert
</code></pre>

<p>So the error message simply tells you that this data is not valid.  We cannot tell without more information what the data should be, and neither can Python or your program do that.</p>

<p>For a rough parable, you can g??ss which b?t?s are m?ss?ng here, but not in ?h?? l?ng?? s???e???.</p>

python encoding gb2312