On Tuesday 27 September 2005 08:32 am, Ian Vincent wrote: > I have a webpage with a BZ2 compressed text embedded in it looking like: > > 'BZh91AY&SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3M > \x07<]\xc9\x14\xe1BA\x06\xbe\x084' > > Now, if I simply copy and paste this into Python and decompress it - it > works a treat. > > However, I want to read the file containing this data, extract the data > and decompress it and this for some reason does not work. > [...] > This gives me a user string of: > > BZh91AY&SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3M > \x07<]\xc9\x14\xe1BA\x06\xbe\x084 > > But if I put this into the decompression function, I get a error of > 'IOError: invalid data stream'. > > I know it is the escape characters but how do I get these to be correctly > converted into a string compatible with bz2.decompress()?
Took me a long time to figure out what you meant. ;-) So the string actually contains the backslashes, not the escaped characters. This works: >>> bz2.decompress(eval(repr(user))) 'huge' (which I take it is what your sample data encoded -- though I can't help but notice it is actually much shorter than the "compressed" version. ;-)). This may have some security issues, though, since it evaluates essentially any expression given for user. I'd be interested to know if someone knows a more secure way. Cheers, Terry -- Terry Hancock ( hancock at anansispaceworks.com ) Anansi Spaceworks http://www.anansispaceworks.com -- http://mail.python.org/mailman/listinfo/python-list