Python Challenge 第6关

地址:http://www.pythonchallenge.com/pc/def/channel.html

代码

from urllib import request
import io
import re
import zipfile

file_name_next = "90052"
comment = ''

zip_get = request.urlopen("http://www.pythonchallenge.com/pc/def/channel.zip")
zip_file = io.BytesIO(zip_get.read())
channel_zip = zipfile.ZipFile(zip_file, "r")

while True:
    file_comment = channel_zip.getinfo(file_name_next+".txt").comment.decode("utf-8")
    comment += file_comment
    with channel_zip.open(file_name_next+".txt", "r") as f:
        file_content = f.read().decode('utf-8')
    try:
        file_name_next = re.search(r"\d+", file_content).group(0)
    except:
        break

print(comment)

zip_file.close()

运行结果

运行结果

学到的东西

import io

StringIO和BytesIO是在内存中操作str和bytes的方法,使得和读写文件具有一致的接口。

s = StringIO() 
s.write(‘hello’) 
s.write(’ ‘) 
s.write(‘world’) 
s.getvalue() 
‘hello world’

import zipfile

import re

from urllib import request

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容