美文网首页程序员
Python Challenge[6]

Python Challenge[6]

作者: Recgat | 来源:发表于2017-02-08 21:20 被阅读0次

    [Level 6]


    Title: now there are pairs

    图片下方是一个PayPal捐赠的图片链接,无其他信息,查看源码,有两处注释:

    1. <-- zip
    2. The following has nothing to do with the riddle itself. I just
      thought it would be the right point to offer you to donate to the
      Python Challenge project. Any amount will be greatly appreciated.
      -thesamet

    图片上是拉链,英文为zip,源码注释有zip,挺考验脑洞的,修改url中的html为zip后缀,下载到channel.zip文件。打开文件,有readme.txt,打开显示:

    welcome to my zipped list.

    hint1: start from 90052
    hint2: answer is inside the zip

    和Level 4类似,不断读取文件直至找到答案。

    num = '90052'
    suffix = '.txt'
    while True:
      try:
        with open('channel/'+num+suffix,encoding='utf-8') as f:
          data = f.read()
          print(data)#Next nothing is 83831
          num = data.split(' ')[-1]
      except:
        break
    

    最后的提示是Collect the comments.。不懂,查看官方文档,搜索comment无果,搜索zip,有zipfile模块,在该页中搜索comment,出现两处:ZipFile.commentZipInfo.comment。使用第一个无果,使用第二个。

    import zipfile
    comments = []
    with zipfile.ZipFile('channel.zip') as zf:
      while True:
        try:
          zi = zf.getinfo(num+suffix)
          comments.append(zi.comment.decode('utf-8'))
          data = zf.read(num+suffix).decode('utf-8')
          num = data.split(' ')[-1]
        except:
          break
    print(''.join(comments))
    

    得到

    result
    大大的hockey,修改url,hockey.html显示it's in the air. look at the letters.,每个大的字母都是由相同的字母组合成,即oxygen,正好对应提示,[Level 7]

    小结

    1. zipfile.ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True)返回ZipFile对象,可用于读取压缩的文件。
    2. ZipFile.getinfo(name)返回ZipInfo对象。
    3. ZipInfo.comment返回成员的注释。
    4. ZipFile.read(name, pwd=None)读取其中的压缩文件。

    Python Challenge Wiki

    一种不下载channel.zip文件的方法:

    import io,zipfile,urllib.request
    zf = zipfile.ZipFile(io.BytesIO(urllib.request.urlopen('http://pythonchallenge.com/pc/def/channel.zip').read()))

    
    1. [`class io.BytesIO([initial_bytes])`](https://docs.python.org/3/library/io.html?highlight=bytesio#io.BytesIO)可以像在二进制模式下打开的文件一样使用。
    
    ####[More](http://wiki.pythonchallenge.com/index.php?title=Level6:Main_Page)

    相关文章

      网友评论

        本文标题:Python Challenge[6]

        本文链接:https://www.haomeiwen.com/subject/nbcbittx.html