Note that there are some explanatory texts on larger screens.

plurals
  1. POData encoding in python
    primarykey
    data
    text
    <p>I download an image from newsgroups with the nntplib module in python. I then want to save the data to the file. I use: </p> <pre><code>news.group('alt.binaries.misc') data=''.join(news.body('&lt;DhTgplpHcRsZMBTTw3i35@spot.net&gt;')[-1]) f=open('image.png','wb') f.write(data) f.close() </code></pre> <p>However, the saved file isn't a proper image file.<br> <code>data</code> is a string of the form:</p> <pre><code>'\x89PNG=B=C\x1a=C=A=A=A=BIHDR=A=A\x02X=A=A\x01Q\x08\x06=A=A=A\xa8\x81\xd3\x89=A=A=A\tpHYs=A=A\x0b\x13=A=A\x0b\x13\x01=A\x9a\x9c\x18=A=A etc... ' </code></pre> <p>, with a lenght of 309530.<br> I can tell from the first bytes that the file should be a png file and the size also seems good to me, so I assume that the data is correct.<br> Does anyone knows what i'm doing wrong?</p> <p>UPDATE:<br> I looked in the header of the article and its says: <code>Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit</code> I don't think this is very helpful with decoding the text, but who knows.. </p> <p>I also compared my string with regular headers of png files. This is <code>\x89PNG\r\n\x1a\n</code>, or <code>\x89PNG\x0d\x0a\x1a\x0a</code>. (as alexis also stated)<br> I concluded that =B stands for \x0d, =C for \x0a and =A for \x00. <strike>I assume that the other \x..'s are not encoded, but i'm not sure (I don't know very much about encodings)</strike> update3 shows that they do differ.<br> What is an encoding that encodes this way?</p> <p>UPDATE2: the data: <i>-see below-</i> (<code>repr(data)</code>)</p> <p>UPDATE3: I was able to save the image with another program and then to open it in python. This is what the data should be. <i>-see below-</i>. The beginnings look kind of similar, but after that there is a big difference. What the hell is this encoding? it really frustrates me. (BTW, thanks for all the great help so far)</p> <hr> <p>All the files: <a href="http://dl.dropbox.com/u/1499291/python-encoding-question/index.html" rel="nofollow">http://dl.dropbox.com/u/1499291/python-encoding-question/index.html</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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