【python zip()和zip(*)方法】教程文章相关的互联网学习教程文章

如何在python中直接将文件添加到zip?【代码】

我是python的新手.我的目标是将数据放入zip文件中.以下是我编写的代码,其中我将数据写入unzipped_file,然后在zipped_file.zip中编写unzipped_file,然后删除解压缩的文件.import os import zipfile##Some code above............. for some_data in big_data:with open('unzipped_file', 'a+') as unzipped_f:unzipped_f.write(some_data)##Some code in between...........with zipfile.ZipFile('zipped_file.zip', 'w') as zipped...

在Python 2.7中的zip和groupby好奇心【代码】

有人可以解释为什么这些在Python 2.7.4中输出不同的东西吗?它们在python 3.3.1中输出相同的内容.我只是想知道这是否是2.7中修复为3的错误,或者是否是由于语言的某些变化.>>> for (i,j),k in zip(groupby([1,1,2,2,3,3]), [4,5,6]): ... print list(j) ... [] [] [3] >>> for i,j in groupby([1,1,2,2,3,3]): ... print list(j) ... [1, 1] [2, 2] [3, 3]解决方法:这不是一个错误.它与groupby iterable消耗时有关.使用py...

python的zip()函数

zip() 函数用于将可迭代对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象。 如果各个可迭代对象的元素个数不一致,则返回的对象长度与最短的可迭代对象相同。 利用 * 号操作符,与zip相反,进行解压。 zip() 函数语法:1zip(iterable1,iterable2, ...)参数说明:iterable -- 一个或多个可迭代对象(字符串、列表、元祖、字典) Python2中:1 2 3 4 5 6 7 8 9 10>>>a?= [1,2,3]?#此处可迭代对象为...

python – 获取远程zip文件并列出其中的文件【代码】

我正在开发一个小型Google App Engine项目,我需要从URL中获取远程zip文件,然后列出zip存档中包含的文件. 我正在使用zipfile模块. 这是我到目前为止所提出的:# fetch the zip file from its remote URL result = urlfetch.fetch(zip_url)# store the contents in a stream file_stream = StringIO.StringIO(result.content)# create the ZipFile object zip_file = zipfile.ZipFile(file_stream, 'w')# read the files by name arc...

python常用函数进阶(2)之map,filter,reduce,zip

Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterable object # iterable : such as list, tuple, string and other iterable objectmap(fun, *iterable) # * token means that multi iterables is supported 1.2 Working map() applying the given function to each item of the given iterable object. map() returns an iterable object called "map object". 1....

Python:将文件解压缩到当前工作目录,但不保存zip中的目录结构【代码】

我有一个像这样的zip文件:myArchive.zip | -folder1|--folder2|---myimage.jpg当我尝试提取myimage.jpg时:with zipfile.ZipFile('myArchive.zip', 'r') as zfile:zfile.extract('folder1/folder2/myimage.jpg')我将在我当前工作的目录中获得/folder1/folder2/myimage.jpg 但我只想将myimage.jpg提取到当前工作目录,我该怎么办呢?解决方法:而不是使用extract或extractall,只需获取数据并将其写入您喜欢的任何文件.这是一个代码示...

Python – 将Zip代码作为字符串加载到DataFrame中?【代码】

我正在使用Pandas加载包含邮政编码(例如32771)的Excel电子表格.邮政编码在电子表格中存储为5位数字符串.使用命令将它们拉入DataFrame时…xls = pd.ExcelFile("5-Digit-Zip-Codes.xlsx") dfz = xls.parse('Zip Codes')他们被转换成数字.所以’00501’变成了501. 所以我的问题是,我该怎么做: 一个.加载DataFrame并保存存储在Excel文件中的邮政编码的字符串类型? 湾将DataFrame中的数字转换为五位数字符串,例如“501”变成“00501”...

Python没有权限在此服务器上访问/从ZIP返回城市/州【代码】

我想要做的是从邮政编码中检索城市和州.这是我到目前为止所拥有的:def find_city(zip_code):zip_code = str(zip_code)url = 'http://www.unitedstateszipcodes.org/' + zip_codesource_code = requests.get(url)plain_text = source_code.textindex = plain_text.find(">")soup = BeautifulSoup(plain_text, "lxml")stuff = soup.findAll('div', {'class': 'col-xs-12 col-sm-6 col-md-12'})我也尝试使用id =“zip-links”,但这不...

python – 在生成的元组中没有重复值的Zip【代码】

使用zip时,是否有优雅或pythonic方式排除包含重复值的条目? 举个例子:>>> list1 = [0, 1] >>> list2 = [0, 2] >>> zip(list1, list2) [(0, 0), (1, 2)]我想只有第二个元素[(1,2)].目前,我这样做 [x for zip in zip(list1,list2)if len(set(x))== len(x)] 但这感觉有点单调乏味.有一个更好的方法吗? 编辑:我如何将其扩展到一般情况,其中有两个以上的列表?>>> list1 = [0, 1] >>> list2 = [0, 2] >>> list3 = [0, 3] >>> ... >>...

Python zip() 函数【图】

描述 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。 如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。zip 方法在 Python 2 和 Python 3 中的不同:在 Python 3.x 中为了减少内存,zip() 返回的是一个对象。如需展示列表,需手动 list() 转换。 如果需要了解 Pyhton3 的应用,可以参考 Python3 zip()。语法...

python – Zipfile模块错误:文件不是zip文件【代码】

我有这个代码:# File: zipfile-example-1.pyimport zipfile,os,globfile = zipfile.ZipFile("Apap.zip", "w")# list filenames for name in glob.glob("C:\Users/*"):print namefile.write(name,os.path.basename(name),zipfile.ZIP_DEFLATED) file = zipfile.ZipFile("Apap.zip", "r") for info in file.infolist():print info.filename, info.date_time, info.file_size, info.compress_size产生此错误:raceback (most recent ...

在for循环中哪个更pythonic:zip或enumerate?【代码】

考虑到可扩展性和可读性,其中哪一个被认为是更加pythonic?使用枚举:group = ['A','B','C'] tag = ['a','b','c']for idx, x in enumerate(group):print(x, tag[idx])或使用zip:for x, y in zip(group, tag):print(x, y)我问的原因是我一直在混合两者.我应该遵循一种标准方法,但它应该是什么?解决方法:毫无疑问,拉链是更加pythonic.它不要求您使用变量来存储索引(您不需要),并且使用它可以统一处理列表,而使用枚举,您迭代一个列表...

使用Python保存下载的ZIP文件【代码】

我正在编写一个脚本,它将自动更新已安装的Calibre版本.目前我已经下载了最新的便携版本.我似乎无法保存zipfile.目前我的代码是:import urllib2 import re import zipfile#tell the user what is happening print("Calibre is Updating")#download the page url = urllib2.urlopen ( "http://sourceforge.net/projects/calibre/files" ).read()#determin current version result = re.search('title="/[0-9.]*/([a-zA-Z\-]*-[0-9\....

使用python opencv从zip加载图像【代码】

我能够从zip成功加载图像:with zipfile.ZipFile('test.zip', 'r') as zfile:data = zfile.read('test.jpg')# how to open this using imread or imdecode?问题是:如何在不保存图像的情况下使用imread或imdecode在opencv中进一步处理? 更新: 这是我得到的预期错误.我需要将’data’转换为opencv可以使用的类型.data = zfile.read('test.jpg') buf = StringIO.StringIO(data) im = cv2.imdecode(buf, cv2.IMREAD_GRAYSCALE) # re...

python – 从元组列表传递参数的zip生成器【代码】

我有一个功能:def func(i, k):j = 0while True:yield j * i + kj += 1还有一些i和k实例:pars = [(2, 4), (1, 5), (7, 2)]如何在不知道pars的长度的情况下压缩pars的func?像这样:for func_tups in zip(func(2, 4), func(1, 5), func(7, 2)):print func_tups我想象地图,拉链,lambda,*的一些组合?解决方法:您正在寻找itertools.starmap():from itertools import starmapfor func_tups in zip(*starmap(func, pairs)):# warning,...