当前位置: 首页>>代码示例>>Python>>正文


Python ZipFile.open方法代码示例

本文整理汇总了Python中zipfile.ZipFile.open方法的典型用法代码示例。如果您正苦于以下问题:Python ZipFile.open方法的具体用法?Python ZipFile.open怎么用?Python ZipFile.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在zipfile.ZipFile的用法示例。


在下文中一共展示了ZipFile.open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: handle_label

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import open [as 别名]
    def handle_label(self, label, **options):
        zip = ZipFile(label)

        map = {}

        map['users'] = self.import_users(zip.open("Users.xml"))
        map['questions'], map['answers'] = self.import_posts(zip.open("Posts.xml"))
开发者ID:EzerchE,项目名称:askbot-devel,代码行数:9,代码来源:sximport.py

示例2: populate

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import open [as 别名]
def populate(show):
    if show in populated:
        return
    else:
        populated.append(show)
    showId = getTvdbId(show)
    endpoint = 'http://thetvdb.com/api/{apikey}/series/{showId}/all/en.zip'.format(apikey=api_key, showId=showId)
    r = requests.get(endpoint)
    z = ZipFile(BytesIO(r.content))
    dataFile = z.open('en.xml')
    data = BeautifulSoup(dataFile.read(),'lxml')
    dataFile.close()
    bannerFile = z.open('banners.xml')
    banners = BeautifulSoup(bannerFile.read(),'lxml')
    bannerFile.close()

    box_url = ''
    for b in banners.find_all('banner'):
        if b.bannertype.string == 'fanart':
            box_url = 'http://thetvdb.com/banners/'+b.bannerpath.string
            break
    localCache[show]['box_url'] = str(box_url)[:]
    seasons = set([int(s.string) for s in data.find_all('seasonnumber') if s.string != '0'])
    for season in seasons:
        localCache[show]['seasons'][int(season)+0] = {'episodes':{}}
        episodes = [e for e in data.find_all('episode') if int(e.seasonnumber.string) == season]
        for episode in episodes:
            number = int(episode.episodenumber.string)+0
            epName = str(episode.episodename.string)[:]
            epThumb = 'http://thetvdb.com/banners/'+str(episode.filename.string)[:]
            localCache[show]['seasons'][season]['episodes'][int(number)+0] = {
                'name':str(epName)[:],
                'thumb_url':str(epThumb)[:]
            }
    close()
开发者ID:antonpaquin,项目名称:Vegarails,代码行数:37,代码来源:tvdb.py

示例3: parse

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import open [as 别名]
 def parse(self, file):
     epub = ZipFile(file)
     if epub.read('mimetype') != 'application/epub+zip'.encode('ascii'):
         raise BadEPub
     with epub.open('META-INF/container.xml') as container_file:
         container = etree.parse(container_file).getroot()
     rootfiles = container.find('./cnt:rootfiles', NS_MAP)
     for rootfile in rootfiles.findall('./cnt:rootfile', NS_MAP):
         if rootfile.get('media-type') != 'application/oebps-package+xml':
             raise BadEPub
         content_path = rootfile.get('full-path')
         break   # only try the first rootfile
     content_dir = os.path.dirname(content_path)
     flowables = []
     with epub.open(content_path) as content_file:
         package = etree.parse(content_file).getroot()
         metadata = package.find('./opf:metadata', NS_MAP)
         print(metadata.find('./dc:title', NS_MAP).text)
         print(metadata.find('./dc:creator', NS_MAP).text)
         manifest = package.find('./opf:manifest', NS_MAP)
         items = {item.get('id'): item
                  for item in manifest.findall('./opf:item', NS_MAP)}
         spine = package.find('./opf:spine', NS_MAP)
         for itemref in spine.findall('./opf:itemref', NS_MAP):
             item = items[itemref.get('idref')]
             filename = os.path.join(content_dir, item.get('href'))
             if filename.endswith('pt04.html'):
                 break
             print(filename)
             with epub.open(filename) as xhtml_file:
                 xhtml_parser = elementtree.Parser(CustomElement)
                 xhtml_tree = xhtml_parser.parse(xhtml_file)
             for flowable in self.from_doctree(xhtml_tree.getroot()):
                 flowables.append(flowable)
     return flowables
开发者ID:ascribe,项目名称:rinohtype,代码行数:37,代码来源:__init__.py

示例4: read_jar

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import open [as 别名]
def read_jar(jar):
    zf = symname = version = lastmod = cmtid = cmttime = origin = cmtdesc = branch = None
    try:
        zf = ZipFile(jar)
        manifest_f = None
        try:
            manifest_f = zf.open('META-INF/MANIFEST.MF')
            manifest = manifest_f.read()
            symname = re_search(r'Bundle-SymbolicName: (.*?)(?:\s|;)', manifest)
            version = re_search(r'Bundle-Version: (.*?)(?:\s|;)', manifest)
            lastmod = re_search(r'Bnd-LastModified: (.*?)(?:\s|;)', manifest)
        except:
            traceback.print_exc()
        finally: 
            if manifest_f: manifest_f.close()
        gitprops_f = None
        try:
            gitprops_f = zf.open('git.properties')
            gitprops = gitprops_f.read()
            cmtid = re_search(r'git.commit.id.abbrev=(.*?)\n', gitprops)
            cmttime = re_search(r'git.commit.time=(.*?)\n', gitprops)
            origin = re_search(r'git.remote.origin.url=(.*?)\n', gitprops)
            cmtdesc = re_search(r'git.commit.id.describe=(.*?)\n', gitprops)
            branch = re_search(r'git.branch=(.*?)\n', gitprops)
        except KeyError:
            pass
        except:
            traceback.print_exc()
        finally:
            if gitprops_f: gitprops_f.close()
    finally:
        if zf: zf.close()
    return (symname, version, lastmod, cmtid, cmttime, origin, cmtdesc, branch)
开发者ID:araqne,项目名称:eediom-devtools,代码行数:35,代码来源:create-cache.py

示例5: unzip

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import open [as 别名]
def unzip(filename):
    z = ZipFile(filename)
    names = z.namelist()
    for path in names:
        if path.startswith('__MACOSX/'):
            continue

        base, name = os.path.split(path)

        if name.startswith('._') and\
            '%s/' % name.replace('._', '', 1) in names:
            continue

        double = os.path.join('__MACOSX', base, '._' + name)
        if double in names:
            print '=> %s.bin' % path

            info = z.getinfo(path)

            bin = MacBinary(name)
            bin.data = z.open(path, 'r').read()
            bin.res = z.open(double, 'r').read()

            modified = datetime.datetime(*info.date_time)
            bin.modified = time.mktime(modified.timetuple())
            bin.created = time.time()

            if not os.path.exists(base):
                os.makedirs(base)

            with open('%s.bin' % path.rstrip('\r'), 'wb') as f:
                f.write(bin.encode())
        else:
            print '-> %s' % path
            z.extract(path)
开发者ID:adamcowan,项目名称:meta,代码行数:37,代码来源:macbinary.py

示例6: main

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import open [as 别名]
def main():
    parser = argparse.ArgumentParser('Update the icomoon icon font from the provided archive')
    parser.add_argument('archive', help='Path to .zip file generated by icomoon')
    args = parser.parse_args()

    script_dir = os.path.dirname(os.path.abspath(__file__))
    vendor_style_dir = script_dir + '/../h/static/styles/vendor'

    icon_font_archive = ZipFile(args.archive)
    icon_font_archive.extract('selection.json', vendor_style_dir + '/fonts')
    icon_font_archive.extract('fonts/h.woff', vendor_style_dir)
    css_input_file = icon_font_archive.open('style.css')

    css_output_file = open(vendor_style_dir + '/icomoon.css', 'w')

    for line in css_input_file:
        if "format('woff')" in line:
            # inline the WOFF format file
            woff_content = icon_font_archive.open('fonts/h.woff').read()
            woff_src_line = """
    /* WARNING - the URL below is inlined
     * because the CSS asset pipeline is not correctly rebasing
     * URLs when concatenating files together.
     *
     * See issue #2571
     */
    src:url('data:application/font-woff;base64,%s') format('woff');
"""
            css_output_file.write(woff_src_line % b64encode(woff_content))
        elif "url(" in line:
            # skip non-WOFF format fonts
            pass
        else:
            css_output_file.write(line)
开发者ID:chr7stos,项目名称:Webmarks,代码行数:36,代码来源:update-icon-font.py

示例7: _read

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import open [as 别名]
    def _read(self) :
        global MANIFEST_FNAME

        z = ZipFile(self.fname, 'r', compression=self.compression)
    
        def _err(msg) :
            z.close()
            raise GluttonImportantFileNotFoundError(msg)
    
        # without the manifest all is lost
        # we need this to get the names of the other
        # XML files
        if MANIFEST_FNAME not in z.namelist() :
            _err('manifest not found in %s' % self.fname)

        self.metadata = json.load(z.open(MANIFEST_FNAME))
        
        self.log.info("read manifest - created on %s using glutton version %.1f" % \
            (time.strftime('%d/%m/%y at %H:%M:%S', time.localtime(self.download_time)), \
             self.version))

        # the data file is the raw data grouped into gene families
        # when we do a local alignment we need to get the gene id
        # of the best hit and find out which gene family it belongs to 
        if self.metadata['data-file'] not in z.namelist() :
            _err('data file (%s) not found in %s' % (self.metadata['data-file'], self.fname))

        self.data = json_to_glutton(json.load(z.open(self.metadata['data-file'])))
        self.seq2famid = self._create_lookup_table(self.data)

        self.log.info("read %d gene families (%d genes)" % (len(self.data), len(self.seq2famid)))

        z.close()
开发者ID:ajm,项目名称:glutton,代码行数:35,代码来源:db.py

示例8: handle_noargs

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import open [as 别名]
    def handle_noargs(self, **options):

        is_verbose = options['verbosity'] > 0

        if is_verbose:
            print "Syncing into", self._static_root
            print "Getting zip from ", self.ZIP_URL

        zip_url = urllib2.urlopen(self.ZIP_URL)
        zip_file = ZipFile(StringIO(zip_url.read()))

        for member in zip_file.namelist():
            # we take only dist, css, and img directories
            dir_name, file_name = os.path.split(member)

            # skip directories
            if not file_name:
                continue

            _, base_dir = os.path.split(dir_name)

            if file_name == self._html_file:
                if is_verbose:
                    print "Adopting ", self._html_file

                # adopt the html to template
                source = zip_file.open(member)
                content = "{% load static from staticfiles %}\n" + source.read()

                for orig, replacement in self._replacements:
                    content = content.replace(orig, replacement)

                source.close()

                target = os.path.join(self._templates_root, 'agendas',
                                      self._html_file)

                with open(target, 'w') as f:
                    f.write(content)

            elif base_dir in self._DIRS:
                target_dir = self._DIRS[base_dir]
                if is_verbose:
                    print "Copying {0} to {1}".format(member, target_dir)

                # make sure we have the target_dir dir
                try:
                    os.makedirs(target_dir)
                except OSError:
                    pass

                source = zip_file.open(member)
                target = file(os.path.join(target_dir, file_name), "wb")

                shutil.copyfileobj(source, target)

                source.close()
                target.close()
开发者ID:benjamingr,项目名称:Open-Knesset,代码行数:60,代码来源:sync_agenda_viz.py

示例9: Feed

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import open [as 别名]
class Feed(object):
    """A collection of CSV files with headers, either zipped into an archive
    or loose in a folder."""

    def __init__(self, filename, strip_fields=True):
        self.filename = filename
        self.feed_name = derive_feed_name(filename)
        self.zf = None
        self.strip_fields = strip_fields
        self.empty_to_none = True
        if not os.path.isdir(filename):
            self.zf = ZipFile(filename)
        if six.PY2:
            self.reader = self.python2_reader
        else:
            self.reader = self.python3_reader

    def __repr__(self):
        return '<Feed %s>' % self.filename

    def python2_reader(self, filename):
        if self.zf:
            try:
                binary_file_handle = self.zf.open(filename, 'rU')
            except IOError:
                raise IOError('%s is not present in feed' % filename)
        else:
            binary_file_handle = open(os.path.join(self.filename, filename),
                                      "rb")
        reader = csv.reader(binary_file_handle)
        for row in reader:
            yield [six.text_type(x, 'utf-8') for x in row]

    def python3_reader(self, filename):
        if self.zf:
            try:
                text_file_handle = io.TextIOWrapper(
                    self.zf.open(filename, "r"), encoding="utf-8")
            except IOError:
                raise IOError('%s is not present in feed' % filename)
        else:
            text_file_handle = open(os.path.join(self.filename, filename), "r",
                                    encoding="utf-8")
        return csv.reader(text_file_handle)

    def read_table(self, filename, columns):
        if self.strip_fields:
            rows = (_row_stripper(row) for row in self.reader(filename))
        else:
            rows = self.reader(filename)
        if self.empty_to_none:
            # Set empty strings to None, let nullable handle missing values.
            rows = ((x if x else None for x in row) for row in rows)
        feedtype = filename.rsplit('/')[-1].rsplit('.')[0].title().replace('_',
                                                                           '')
        return CSV(feedtype=feedtype, rows=rows, columns=columns)
开发者ID:jarondl,项目名称:pygtfs,代码行数:58,代码来源:feed.py

示例10: read_single_sheet

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import open [as 别名]
def read_single_sheet(path, name=None):
    """ Read an xlsx, csv or tsv from a zipfile or directory
    """
    from zipfile import ZipFile
    import xlreader

    if name is None:
        root, ext = os.path.splitext(path)
        stream = open(path, 'rb')

        if ext == '.xlsx':
            return read_xl(stream)

        if ext == '.tsv':
            return read_csv(stream, dialect='excel-tab')

        if ext == '.csv':
            return read_csv(stream)

        raise ValueError('Unknown file extension for %r' % path)

    if path.endswith('.xlsx'):
        return xlreader.DictReader(open(path, 'rb'), sheetname=name)

    if path.endswith('.zip'):
        zf = ZipFile(path)
        names = zf.namelist()

        if (name + '.xlsx') in names:
            stream = zf.open(name + '.xlsx', 'r')
            return read_xl(stream)

        if (name + '.tsv') in names:
            stream = zf.open(name + '.tsv', 'rU')
            return read_csv(stream, dialect='excel-tab')

        if (name + '.csv') in names:
            stream = zf.open(name + '.csv', 'rU')
            return read_csv(stream)

    if os.path.isdir(path):
        root = os.path.join(path, name)

        if os.path.exists(root + '.xlsx'):
            stream = open(root + '.xlsx', 'rb')
            return read_xl(stream)

        if os.path.exists(root + '.tsv'):
            stream = open(root + '.tsv', 'rbU')
            return read_csv(stream, dialect='excel-tab')

        if os.path.exists(root + '.csv'):
            stream = open(root + '.csv', 'rbU')
            return read_csv(stream)

    return []
开发者ID:zhouyu,项目名称:encoded,代码行数:58,代码来源:loadxl.py

示例11: __call__

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import open [as 别名]
 def __call__(self, zipfile):
     zipfile = ZipFile(zipfile)
     filenames = zipfile.namelist()
     xls_files = [x for x in filenames if x.endswith('xls')]
     doc_files = [x for x in filenames if x.endswith('doc')]
     if len(xls_files) > 1:
         raise Exception(_("Zip file contains too many excel files"))
     if not xls_files:
         raise Exception(_("Zip file contains no excel files"))
     return StringIO(zipfile.open(xls_files[0]).read()), [StringIO(zipfile.open(x).read()) for x in doc_files]
开发者ID:syslabcom,项目名称:recensio.imports,代码行数:12,代码来源:zip_extractor.py

示例12: testConvertDocy

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import open [as 别名]
  def testConvertDocy(self):
    """Test conversion of docy to docx and back"""
    x_data = Handler(self.tmp_url, open("data/test_with_image.docy").read(), "docy", **self.kw).convert("docx")
    self.assertIn("word/", x_data[:2000])

    y_data = Handler(self.tmp_url, x_data, "docx", **self.kw).convert("docy")
    y_zip = ZipFile(StringIO(y_data))
    y_body_data = y_zip.open("body.txt").read()
    self.assertTrue(y_body_data.startswith("DOCY;v10;0;"), "%r... does not start with 'DOCY;v10;0;'" % (y_body_data[:20],))
    y_zip.open("media/image1.png")
开发者ID:Nexedi,项目名称:cloudooo,代码行数:12,代码来源:testX2tHandler.py

示例13: testConvertDocx

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import open [as 别名]
  def testConvertDocx(self):
    """Test conversion of docx to docy and back"""
    y_data = Handler(self.tmp_url, open("data/test_with_image.docx").read(), "docx", **self.kw).convert("docy")
    y_zip = ZipFile(StringIO(y_data))
    y_body_data = y_zip.open("body.txt").read()
    self.assertTrue(y_body_data.startswith("DOCY;v10;0;"), "%r... does not start with 'DOCY;v10;0;'" % (y_body_data[:20],))
    y_zip.open("media/image1.png")

    x_data = Handler(self.tmp_url, y_data, "docy", **self.kw).convert("docx")
    # magic inspired by https://github.com/minad/mimemagic/pull/19/files
    self.assertIn("word/", x_data[:2000])
开发者ID:Nexedi,项目名称:cloudooo,代码行数:13,代码来源:testX2tHandler.py

示例14: import_gtfs

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import open [as 别名]
    def import_gtfs(self, gtfs_file, verbose=False):
        """Import a GTFS file as feed

        Keyword arguments:
        gtfs_file - A path or file-like object for the GTFS feed

        Returns is a list of objects imported
        """
        z = ZipFile(gtfs_file, 'r')
        files = z.namelist()

        gtfs_order = (
            ('agency.txt', Agency),
            ('stops.txt', Stop),
            ('routes.txt', Route),
            ('calendar.txt', Service),
            ('calendar_dates.txt', ServiceDate),
            ('shapes.txt', ShapePoint),
            ('trips.txt', Trip),
            ('stop_times.txt', StopTime),
            ('frequencies.txt', Frequency),
            ('fare_attributes.txt', Fare),
            ('fare_rules.txt', FareRule),
            ('transfers.txt', Transfer),
            ('feed_info.txt', FeedInfo),
        )

        post_save.disconnect(dispatch_uid='post_save_shapepoint')
        post_save.disconnect(dispatch_uid='post_save_stop')
        try:
            for table_name, klass in gtfs_order:
                for f in files:
                    if f.endswith(table_name):
                        table = z.open(f, 'rU')
                        if verbose:
                            rows = len(list(csv.reader(table)))
                            print("importing {x} rows of {table}".format(x=rows, table=table_name))

                        table = z.open(f, 'rU')
                        klass.import_txt(table, self, verbose=verbose)
        finally:
            post_save.connect(post_save_shapepoint, sender=ShapePoint)
            post_save.connect(post_save_stop, sender=Stop)

        # Update geometries
        print("updating geometries...")
        # TODO: Add test feed that includes shapes (issue #20)
        for shape in self.shape_set.all():  # pragma: no cover
            shape.update_geometry(update_parent=False)
        for trip in Trip.objects.in_feed(self):
            trip.update_geometry(update_parent=False)
        for route in self.route_set.all():
            route.update_geometry()
开发者ID:UrbanFootprint,项目名称:django-multi-gtfs,代码行数:55,代码来源:feed.py

示例15: configure_search_replace

# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import open [as 别名]
def configure_search_replace(request):
    if request.method == 'GET':
        zf_in = ZipFile(request.session['stored_archive_filename'], mode='r')
        all_filenames_lst = zf_in.namelist()
        all_filenames = set(all_filenames_lst)
        assert len(all_filenames) == len(all_filenames_lst), "Duplicate filenames in the input file?!"
        zf_in.close()
        return render_to_response('docx_search_replace/configure_search_replace.html',
                                  {'filenames': sorted(all_filenames)})
    elif request.method == 'POST':
        replacements = []
        for i in range(1, 6):  # We have input fields "from1", "to1"... "from5", "to5"
            if request.POST['from%d' % i]:
                replacements.append((request.POST['from%d' % i], request.POST['to%d' % i]))
        logging.info('replacements: %s' % replacements)

        selected_filenames = [k for k in request.POST if request.POST[k] == 'on']
        logging.info('selected_filenames: %s' % selected_filenames)

        zf_in = ZipFile(request.session['stored_archive_filename'], mode='r')
        all_filenames = zf_in.namelist()
        stored_output_file = tempfile.NamedTemporaryFile(delete=False)
        zf_out = ZipFile(stored_output_file.name, mode='w', compression=zf_in.compression)

        for fname in selected_filenames:
            file_contents = zf_in.open(fname).read().decode('utf-8')
            for r in replacements:
                file_contents = file_contents.replace(*r)
            zf_out.writestr(fname, file_contents.encode('utf-8'))

        filenames_to_copy_unchanged = set(all_filenames) - set(selected_filenames)
        for fname in filenames_to_copy_unchanged:
            zf_out.writestr(fname, zf_in.open(fname).read(), compress_type=ZIP_DEFLATED)

        zf_in.close()
        zf_out.close()

        orig_uploaded_filename = request.session['uploaded_filename']
        if orig_uploaded_filename.endswith('.docx'):
            downloading_filename = re.sub('.docx$', '_EDITED.docx', orig_uploaded_filename)
        else:
            downloading_filename = orig_uploaded_filename + '_EDITED'

        ret_file = open(stored_output_file.name, 'rb')
        resp = HttpResponse(status=200,
                content=ret_file.read(),
                mimetype='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
        resp['Content-Disposition'] = 'attachment; filename="%s"' % downloading_filename
        return resp

    else:
        return HttpResponseBadRequest('Invalid method: %s' % request.method)
开发者ID:DougAK,项目名称:docx-searchreplace,代码行数:54,代码来源:views.py


注:本文中的zipfile.ZipFile.open方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。