I'm learning python by doing the python challenge using python3.3 and I'm on question eight. There's a comment in the markup providing you with two bz2-compressed unicode strings outputting byte strings, one for username and one for password. There's also a link where you need the decompressed credentials to enter.
One way to easily solve this is just to manually copy the strings and assign it to two variables as byte strings and then just use the bz2 library to decompress it:
>>>import bz2, re, requests
>>>url = requests.get('http://www.pythonchallenge.com/pc/def/integrity.html')
>>>un = re.findall(r'un: \'(.*)\'',url.text)
>>>print(un,un is correct,sep='\n')
The backslashes are present in the HTML source, so it's not surprising that the
requests module preserves them. I don't have
requests installed on my Python 3 environment, so I haven't been able to exactly replicate your situation, but it looks to me like if you start capturing the surrounding
' characters, you can use
ast.literal_eval to parse the character sequence into a bytes array:
>>> test "'BZh91AY&SYA\\xaf\\x82\\r\\x00\\x00\\x01\\x01\\x80\\x02\\xc0\\x02\\x00 \\x00!\\x9ah3M\\x07<]\\xc9\\x14\\xe1BA\\x06\\xbe\\x084'" >>> import ast >>> res = ast.literal_eval("b%s" % test) >>> import bz2 >>> len(bz2.decompress(res)) 4
There are probably other ways, but why not use Python's built in knowledge that the byte sequence
b'\\xaf' can be parsed into a bytes array?