codecamp

Python菜谱14:从.zip文件中读取数据

Python 能够直接读取 zip 文件中的数据。我们现在需要实现这样一个小任务:直接读取一个 zip 文件,获取里面包含的文件列表以及每个文件的大小。

Python 的 zipfile 模块可以轻松地帮助我们解决这个任务:

import zipfile

z = zipfile.ZipFile("test.zip", "r")
for filename in z.namelist():
        print filename
        bytes = z.read(filename)
        print len(bytes)

这里需要注意地是 zipfile 模块有些 zip 文件是无法处理,具体是里面插入了注释的 zip 文件或者多分卷的 zip 文件。

Python菜谱13:在文件中搜索以及替换文本
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }