閱讀28 返回首頁    go 阿裏雲 go 技術社區[雲棲]


pythonchallenge_level6

level6

地址:https://www.pythonchallenge.com/pc/def/channel.html
源碼:git@code.aliyun.com:qianlizhixing12/PythonChallenge.git。
問題:使用zipfile對象觀察打印。

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

# Level 6

import os
import urllib.request

filename = "channel.zip"
url = "https://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)

最後更新:2017-10-01 07:33:29

  上一篇:go  pythonchallenge_level7
  下一篇:go  pythonchallenge_level5