Python Challenge Level 6

时间:2014-12-20 15:34:14   收藏:0   阅读:265

惭愧,这一题是参考别人的博客做出来的,主要的难点在

1.怎么得到zip文件

2.comments是个什么东西,原作者也在博客中吐槽“ what the hell are the comments?"

从这一题中我发现

1.提示信息不一定只在本网页,可以适当修改网址获取另外的信息。

2.对于提示中你所不能理解的信息如本题中的”comments“可以尝试在python doc中搜索(这一点往往是解题的关键),还有python doc可以在你的安装文件夹中找到

代码如下:

 1 # http://www.pythonchallenge.com/pc/def/channel.html
 2 # author: nzh@cs.tju
 3 import os
 4 import sys
 5 import zipfile
 6 import re
 7 import urllib.request
 8 import urllib.response
 9 def DownloadFile(url):
10     response = urllib.request.urlopen(url)
11     zp = open(channel.zip,wb)
12     zp.write(response.read())
13     zp.close()
14 
15 def Analysis(chZip,arcname):
16     zp = zipfile.ZipFile(chZip,r)
17     zp.extractall(path=arcname)
18     zp.close()
19     flist = os.listdir(arcname)
20     for f in flist:
21         txfile = open(arcname+f,r)
22         for line in txfile:
23             print(line)
24 
25 def Flow(chZip,beg):
26     zp = zipfile.ZipFile(chZip,r)
27     nameList = zp.namelist()
28     nowFile = beg
29     dirPath = ./channel/
30     pat = re.compile(\d{1,5})
31     comments = ‘‘
32     while True:
33         fname = nowFile + .txt
34         comments += str(zp.getinfo(fname).comment,encoding=utf-8)
35         zp.extract(fname,path=dirPath)
36         f = open(dirPath + fname,r)
37         content = f.readline()
38         matchs = pat.findall(content)
39 
40         if len(matchs) != 0:
41             nowFile = matchs[0]
42         else:
43             break
44     return comments
45 if __name__ == __main__:
46     nowUrl = rhttp://www.pythonchallenge.com/pc/def/channel.zip
47     DownloadFile(nowUrl)
48     #Analysis(‘channel.zip‘,‘./channel/‘)
49     print(Flow(channel.zip,90052))

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!