【发布时间】:2014-12-16 11:25:45
【问题描述】:
我有一个这样的字符串:
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'
这是我的代码:
un = re.search(r"un: '(.+)'",page).group(1)
bz2.decompress(un)
然后我使用bz2.decompress方法,它返回错误:
IOError: invalid data stream
我试试这个:
un = 'BZh91...\x084'
bz2.decompress(un)
它会返回正确的答案。
补充:这是我的完整代码。
#!/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 is copied from output of 'print unstring'
print bz2.decompress (un)
print bz2.decompress (unstring)
if (__name__=="__main__"):
main()
这是输出:
==== 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
>>>
【问题讨论】:
-
prin(repr(un))显示什么?那些\xhh是转义实际字节还是每个 4 个字符长? -
我猜你的 RE 同时采用了
un和pw值,而pw:在中间。解压前尝试打印一下。 -
顺便说一句,我刚刚试过,
pw解压缩正确,但un是错误的。也许你有一个错字...... -
您给我们的示例帖子无效。
-
离题了,但是您是否注意到您刚刚在 Internet 上发布了您的密码? (
pw代表 密码,不是吗?)
标签: python bzip2 compression