【原创】pythonchalleng——第6题

来源:互联网 发布:mac pc区别 编辑:程序博客网 时间:2024/05/17 22:15

        第六题的网址:http://www.pythonchallenge.com/pc/def/channel.html,如果大家访问不了该网站,可以看看下面的网站截图。

2011-07-19_101350

这道题寻找关键信息的出发点:页面标题、源代码注释。通过这两点,我们收集到的信息有:

我们可以下载“http://www.pythonchallenge.com/pc/def/channel.zip”

        把压缩包下载回来后,解压缩,打开README文件,得到如下提示:

welcome to my zipped list.

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

再看看其他文件。原来使用第4题的方法处理这些文件。处理完毕后,得到的结果是“Collect the comments.”这下可把我难坏了……线索到这里就断了……到底让我们收集什么信息呢?而README提示答案就在zip里,但是zip里没有什么额外的信息了……迫于无奈,我借鉴了百度的答案,这才知道,原来出题者是让我们收集zip文件中各文件的comment……悲剧了,不了解zip的文件格式,根本不知道zip中还有这个属性(参见《ZIP文件格式》)。

        好了,既然知道题意了,那我们开始动手吧。还是先看看官方文档。这次我们应该查看和“zip”有关系的模块zipfile

The ZIP file format is a common archive and compression standard. This module provides tools to create, read, write, append, and list a ZIP file. Any advanced use of this module will require an understanding of the format, as defined in PKZIP Application Note.

包含的方法有:close()、getinfo( name) 、infolist( ) 、namelist( ) 、printdir( ) 、read( name)、testzip( )、write( filename[, arcname[, compress_type]])、writestr( zinfo_or_arcname, bytes)。大家一看就应该知道了我们需要使用什么方法。getinfo( name)用来获取各个文件的信息,read( name)用来读取文件内容。需要注意的是,我们应该按照next nothing的顺序收集文件的comment。好了,代码如下:

''' 
Created on 2011-7-19 
@author: hengha 
''' 
import re 
import zipfile 
def getNext(num,url,rex): 
    content='' 
    while Tr: 
        f=open(url+'\\'+num+'.txt') 
        content=f.read() 
        #print content 
        try: 
            p=re.compile(rex) 
            result=p.findall(content) 
            num=result[-1] 
        except: 
            break 
def getComments(num,url,rex): 
    file=zipfile.ZipFile(url+'.zip') 
    comment='' 
    while Tr: 
        content=file.read(num+'.txt') 
        #print content 
        p=re.compile(rex) 
        try: 
            result=p.findall(content) 
            comment+=file.getinfo(num+'.txt').comment 
            num=result[0] 
        except: 
            break 
    file.close() 
    return comment 
if __name__ == '__main__': 
    url="E:\user\Desktop\channel" 
    rex=r'[0-9]+' 
    getNext('90052',url,rex) 
    print getComments('90052',url,rex)

看看我们得到的是什么结果:

**************************************************************** 
**************************************************************** 
**                                                            ** 
**   OO    OO    XX      YYYY    GG    GG  EEEEEE NN      NN  ** 
**   OO    OO  XXXXXX   YYYYYY   GG   GG   EEEEEE  NN    NN   ** 
**   OO    OO XXX  XXX YYY   YY  GG GG     EE       NN  NN    ** 
**   OOOOOOOO XX    XX YY        GGG       EEEEE     NNNN     ** 
**   OOOOOOOO XX    XX YY        GGG       EEEEE      NN      ** 
**   OO    OO XXX  XXX YYY   YY  GG GG     EE         NN      ** 
**   OO    OO  XXXXXX   YYYYYY   GG   GG   EEEEEE     NN      ** 
**   OO    OO    XX      YYYY    GG    GG  EEEEEE     NN      ** 
**                                                            ** 
**************************************************************** 
 **************************************************************

访问http://www.pythonchallenge.com/pc/def/hockey.html,看看我们会看到什么?

it's in the air. look at the letters.

        让我们看字母,哦,应该是oxygen,我们再试一试,http://www.pythonchallenge.com/pc/def/oxygen.html。OK,顺利进入第7题。

0 0
原创粉丝点击