Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>.decode('base64')</code> can be called only on a string that's encoded as "base-64, in order to retrieve the byte sequence that was there encoded. Presumably that byte sequence, in the example you bring, was zlib-compressed, and so the <code>.decode('zlib')</code> part decompresses it.</p> <p>Now, for your case:</p> <pre><code>&gt;&gt;&gt; 'dsss'.decode('base64') 'v\xcb,' </code></pre> <p>But <code>'v\xcv,'</code> is not a zlib-compressed string! And so of course you cannot ask zlib to "decompress" it. Fortunately <code>zlib</code> recognizes the fact (that <code>'v\xcv,'</code> could not possibly have been produced by applying any of the compression algorithms <code>zlib</code> knows about to any input whatsoever) and so gives you a helpful error message (instead of a random-ish string of bytes, which you might well have gotten if you had randomly supplied a different but equally crazy input string!-)</p> <p><strong>Edit</strong>: the error in</p> <pre><code>a.encode('base64') print a a.decode('base64')#error </code></pre> <p>is obviously due to the fact that <strong>strings are immutable</strong>: just calling <code>a.encode</code> (or any other method) does <strong>not</strong> alter <code>a</code>, it produces a new string object (and here you're just printing it).</p> <p>In the next snippet, the error is only in the OP's mind:</p> <pre><code>&gt;&gt;&gt; a='dsss' &gt;&gt;&gt; a=a.encode('zlib') &gt;&gt;&gt; print a x?K)..F? &gt;&gt;&gt; a=a.decode('zlib') &gt;&gt;&gt; print a#why can't print 'dsss' dsss &gt;&gt;&gt; </code></pre> <p>that "why can't print" question is truly peculiar, applied to code that <strong>does</strong> print 'dsss'. Finally, </p> <blockquote> <p>i think the 'print a' encode the a with 'uhf-8'.</p> </blockquote> <p>You think wrongly: there's no such thing as "uhf-8" (you mean "utf-8" maybe?), and anyway <code>print a</code> does <strong>not</strong> alter <code>a</code>, any more than just calling <code>a.encode</code> does.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload