bzip2 - using bz2.decompress in python,but the answers different -
i have string this:
un: 'bzh91ay&sya\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3m\x07<]\xc9\x14\xe1ba\x06\xbe\x084' pw: 'bzh91ay&sy\x94$|\x0e\x00\x00\x00\x81\x00\x03$ \x00!\x9ah3m\x13<]\xc9\x14\xe1bbp\x91\xf08'
and code:
un = re.search(r"un: '(.+)'",page).group(1) bz2.decompress(un)
then use bz2.decompress
method, returns error:
ioerror: invalid data stream
and try this:
un = 'bzh91...\x084' bz2.decompress(un)
and returns correct answer.
supplement:this complete code.
#!/usr/bin/env python import urllib import re import bz2 def main(): page=urllib.urlopen("http://www.pythonchallenge.com/pc/def/integrity.html").read() unstring = re.search(r"un: *'(.+)'",page).group(1) print unstring un = "bzh91ay&sya\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3m\x07<]\xc9\x14\xe1ba\x06\xbe\x084" #the string un copied output of 'print unstring' print bz2.decompress (un) print bz2.decompress (unstring) if (__name__=="__main__"): main()
this output:
==== no subprocess ==== >>> bzh91ay&sya\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3m\x07<]\xc9\x14\xe1ba\x06\xbe\x084 huge traceback (most recent call last): file "/home/terry/pythonchallage/pythonchallenge_8.py", line 16, in <module> main() file "/home/terry/pythonchallage/pythonchallenge_8.py", line 14, in main print bz2.decompress (unstring) ioerror: invalid data stream >>>
you have string literals there, each \xhh
value 4 literal characters, not byte escape.
if so, you'll first need tell python interpret those:
bz2.decompress(un.decode('string_escape'))
demo:
>>> unstring = r'bzh91ay&sya\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3m\x07<]\xc9\x14\xe1ba\x06\xbe\x084' >>> print unstring bzh91ay&sya\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3m\x07<]\xc9\x14\xe1ba\x06\xbe\x084 >>> unstring 'bzh91ay&sya\\xaf\\x82\\r\\x00\\x00\\x01\\x01\\x80\\x02\\xc0\\x02\\x00 \\x00!\\x9ah3m\\x07<]\\xc9\\x14\\xe1ba\\x06\\xbe\\x084' >>> import bz2 >>> bz2.decompress(unstring) traceback (most recent call last): file "<stdin>", line 1, in <module> ioerror: invalid data stream >>> bz2.decompress(unstring.decode('string_escape')) 'huge'
Comments
Post a Comment