Python: fetching SVG file using urllib is returning binary when I need ASCII

Posted by Drew Dara-Abrams on Stack Overflow See other posts from Stack Overflow or by Drew Dara-Abrams
Published on 2010-03-30T19:07:39Z Indexed on 2010/03/30 19:13 UTC
Read the original article Hit count: 517

Filed under:
|
|
|

I'm using urllib (in Python) to fetch an SVG file:

import urllib

urllib.urlopen('http://alpha.vectors.cloudmade.com/BC9A493B41014CAABB98F0471D759707/-122.2487,37.87588,-122.265823,37.868054?styleid=1&viewport=400x231').read()

which produces output of the sort:

xb6\xf6\x00\xb3\xfb2\xff\xda\xc5\xf2\xc2\x14\xef\xcd\x82\x0b\xdbU\xb0\x81\xcaF\xd8\x1a\xf6\xdf[i)\xba\xcf\x80\xab\xd6\x8c\xe3l_\xe7\n\xed2,\xbdm\xa0_|\xbb\x12\xff\xb6\xf8\xda\xd9\xc3\xd9\t\xde\x9a\xf8\xae\xb3T\xa3\r`\x8a\x08!T\xfb8\x92\x95\x0c\xdd\x8b!\x02P\xea@\x98\x1c^\xc7\xda\\\xec\xe3\xe1\xbe,0\xcd\xbeZ~\x92\xb3\xfa\xdd\xfcbyu\xb8\x83\xbb\xbdS\x0f\x82\x0b\xfe\xf5_\xdawn\xff\xef_\xff\xe5\xfa\x1f?\xbf\xffoZ\x0f\x8b\xbfV\xf4\x04\x00'

when I was expecting more like this:

<?xml version='1.0' encoding='UTF-8'?> <svg xmlns="http://www.w3.org/2000/svg" xmlns:cm="http://cloudmade.com/" width="400" height="231">   <rect width="100%" height="100%" fill="#eae8dd" opacity="1"/>   <g transform="scale(0.209849975856)">
    <g transform="translate(13610569, 4561906)" flood-opacity="0.1" flood-color="grey">
      <path d="M -13610027.720000000670552 -4562403.660000000149012

I guess this is an issue of binary vs. ASCII. Can anyone help me (a Python newbie) with the appropriate conversion so that I can get on with parsing and manipulating the SVG code?

© Stack Overflow or respective owner

Related posts about python

Related posts about urllib