欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

pythonchallenge_level6

程序员文章站 2024-02-12 19:59:34
...

level6

地址:http://www.pythonchallenge.com/pc/def/channel.html
源码:[email protected]:qianlizhixing12/PythonChallenge.git。
问题:使用zipfile对象观察打印。

#!/usr/bin/env python3
# -*- coding:UTF-8 -*-

# Level 6

import os
import urllib.request

filename = "channel.zip"
url = "http://www.pythonchallenge.com/pc/def/" + filename
response = urllib.request.urlopen(url)
body = response.read()
response.close

if os.path.exists(filename): os.remove(filename)
tmpzip = open(filename, "wb")
tmpzip.write(body)
tmpzip.close()

import zipfile

tmpzip = zipfile.ZipFile(filename, mode = "r")

sid = "90052"
comments = []
while True:
    sfile = sid + ".txt"
    comments.append(tmpzip.getinfo(sfile).comment.decode(encoding="gbk"))
    sid = tmpzip.read(sfile).decode(encoding="gbk").split(" ")[-1]
    if not sid.isdigit():
        break

print("Level 6:\n", "".join(comments))

if os.path.exists(filename): os.remove(filename)