Skip to main content
 首页 » 编程设计

python之在 python 中使用 bz2.decompress,但答案不同

2024年10月25日11me-sa

<分区>

我有这样一个字符串:

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 
>>>