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


Python bz2.BZ2File方法代码示例

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


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

示例1: bz2open

# 需要导入模块: import bz2 [as 别名]
# 或者: from bz2 import BZ2File [as 别名]
def bz2open(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
        """Open bzip2 compressed tar archive name for reading or writing.
           Appending is not allowed.
        """
        if len(mode) > 1 or mode not in "rw":
            raise ValueError("mode must be 'r' or 'w'.")

        try:
            import bz2
        except ImportError:
            raise CompressionError("bz2 module is not available")

        fileobj = bz2.BZ2File(fileobj or name, mode,
                              compresslevel=compresslevel)

        try:
            t = cls.taropen(name, mode, fileobj, **kwargs)
        except (IOError, EOFError):
            fileobj.close()
            raise ReadError("not a bzip2 file")
        t._extfileobj = False
        return t 
开发者ID:war-and-code,项目名称:jawfish,代码行数:24,代码来源:tarfile.py

示例2: openFile

# 需要导入模块: import bz2 [as 别名]
# 或者: from bz2 import BZ2File [as 别名]
def openFile(filename, modes):
	filetype = file_type(filename)
	if filetype is None:
		return open(filename, modes)
	elif filetype == "bz2":
		return bz2.BZ2File(filename)
	elif filetype == "gz":
		return gzip.open(filename)
	elif filetype == "xz":
		with open(filename, modes) as f:
			return xz.LZMAFile(f)
	elif filetype == "zip":
		return zipfile.ZipFile(filename)
	else:
		# should never get here
		raise LookupError("filetype is invalid") 
开发者ID:hanzhang0116,项目名称:BotDigger,代码行数:18,代码来源:BotDigger.py

示例3: test_Bz2File_text_mode_warning

# 需要导入模块: import bz2 [as 别名]
# 或者: from bz2 import BZ2File [as 别名]
def test_Bz2File_text_mode_warning(self):
        try:
            import bz2
        except ImportError:
            # We don't have the bz2 capabilities to test.
            pytest.skip()
        # Test datasource's internal file_opener for BZip2 files.
        filepath = os.path.join(self.tmpdir, 'foobar.txt.bz2')
        fp = bz2.BZ2File(filepath, 'w')
        fp.write(magic_line)
        fp.close()
        with assert_warns(RuntimeWarning):
            fp = self.ds.open(filepath, 'rt')
            result = fp.readline()
            fp.close()
        assert_equal(magic_line, result) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:18,代码来源:test__datasource.py

示例4: _python2_bz2open

# 需要导入模块: import bz2 [as 别名]
# 或者: from bz2 import BZ2File [as 别名]
def _python2_bz2open(fn, mode, encoding, newline):
    """Wrapper to open bz2 in text mode.

    Parameters
    ----------
    fn : str
        File name
    mode : {'r', 'w'}
        File mode. Note that bz2 Text files are not supported.
    encoding : str
        Ignored, text bz2 files not supported in Python2.
    newline : str
        Ignored, text bz2 files not supported in Python2.
    """
    import bz2

    _check_mode(mode, encoding, newline)

    if "t" in mode:
        # BZ2File is missing necessary functions for TextIOWrapper
        warnings.warn("Assuming latin1 encoding for bz2 text file in Python2",
                      RuntimeWarning, stacklevel=5)
        mode = mode.replace("t", "")
    return bz2.BZ2File(fn, mode) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:26,代码来源:_datasource.py

示例5: save

# 需要导入模块: import bz2 [as 别名]
# 或者: from bz2 import BZ2File [as 别名]
def save(self,filename,compressed=True):
        """
        :param compressed: if C{True}, then save in compressed XML; otherwise, save in XML (default is C{True})
        :type compressed: bool
        :returns: the filename used (possibly with a .psy extension added)
        :rtype: str
        """
        if compressed:
            if filename[-4:] != '.psy':
                filename = '%s.psy' % (filename)
        elif filename[-4:] != '.xml':
            filename = '%s.xml' % (filename)
        if compressed:
            f = bz2.BZ2File(filename,'w')
            f.write(self.__xml__().toprettyxml().encode('utf-8'))
        else:
            f = open(filename,'w')
            f.write(self.__xml__().toprettyxml())
        f.close()
        return filename 
开发者ID:pynadath,项目名称:psychsim,代码行数:22,代码来源:world.py

示例6: _write_data

# 需要导入模块: import bz2 [as 别名]
# 或者: from bz2 import BZ2File [as 别名]
def _write_data(data, filehandle, options):
    # Now write data directly
    #rawdata = data.transpose([2,0,1]).tostring(order = 'C')
    rawdata = data.transpose([2,1,0]).tostring(order = 'C');
    if options['encoding'] == 'raw':
        filehandle.write(rawdata)
    elif options['encoding'] == 'gzip':
        gzfileobj = gzip.GzipFile(fileobj = filehandle)
        gzfileobj.write(rawdata)
        gzfileobj.close()
    elif options['encoding'] == 'bz2':
        bz2fileobj = bz2.BZ2File(fileobj = filehandle)
        bz2fileobj.write(rawdata)
        bz2fileobj.close()
    else:
        raise NrrdError('Unsupported encoding: "%s"' % options['encoding']) 
开发者ID:ChristophKirst,项目名称:ClearMap,代码行数:18,代码来源:NRRD.py

示例7: bz2open

# 需要导入模块: import bz2 [as 别名]
# 或者: from bz2 import BZ2File [as 别名]
def bz2open(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs):
        """Open bzip2 compressed tar archive name for reading or writing.
           Appending is not allowed.
        """
        if len(mode) > 1 or mode not in "rw":
            raise ValueError("mode must be 'r' or 'w'.")

        try:
            import bz2
        except ImportError:
            raise CompressionError("bz2 module is not available")

        if fileobj is not None:
            fileobj = _BZ2Proxy(fileobj, mode)
        else:
            fileobj = bz2.BZ2File(name, mode, compresslevel=compresslevel)

        try:
            t = cls.taropen(name, mode, fileobj, **kwargs)
        except (IOError, EOFError):
            raise ReadError("not a bzip2 file")
        t._extfileobj = False
        return t

    # All *open() methods are registered here. 
开发者ID:glmcdona,项目名称:meddle,代码行数:27,代码来源:tarfile.py

示例8: _python2_bz2open

# 需要导入模块: import bz2 [as 别名]
# 或者: from bz2 import BZ2File [as 别名]
def _python2_bz2open(fn, mode, encoding, newline):
    """Wrapper to open bz2 in text mode.

    Parameters
    ----------
    fn : str
        File name
    mode : {'r', 'w'}
        File mode. Note that bz2 Text files are not supported.
    encoding : str
        Ignored, text bz2 files not supported in Python2.
    newline : str
        Ignored, text bz2 files not supported in Python2.
    """
    import bz2

    _check_mode(mode, encoding, newline)

    if "t" in mode:
        # BZ2File is missing necessary functions for TextIOWrapper
        raise ValueError("bz2 text files not supported in python2")
    else:
        return bz2.BZ2File(fn, mode) 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:25,代码来源:_datasource.py

示例9: test_fwf_compression

# 需要导入模块: import bz2 [as 别名]
# 或者: from bz2 import BZ2File [as 别名]
def test_fwf_compression(self):
        try:
            import gzip
            import bz2
        except ImportError:
            pytest.skip("Need gzip and bz2 to run this test")

        data = """1111111111
        2222222222
        3333333333""".strip()
        widths = [5, 5]
        names = ['one', 'two']
        expected = read_fwf(StringIO(data), widths=widths, names=names)
        if compat.PY3:
            data = bytes(data, encoding='utf-8')
        comps = [('gzip', gzip.GzipFile), ('bz2', bz2.BZ2File)]
        for comp_name, compresser in comps:
            with tm.ensure_clean() as path:
                tmp = compresser(path, mode='wb')
                tmp.write(data)
                tmp.close()
                result = read_fwf(path, widths=widths, names=names,
                                  compression=comp_name)
                tm.assert_frame_equal(result, expected) 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:26,代码来源:test_read_fwf.py

示例10: get_bz2_data

# 需要导入模块: import bz2 [as 别名]
# 或者: from bz2 import BZ2File [as 别名]
def get_bz2_data(data_dir, data_name, url, data_origin_name):
    """Download and extract bz2 data.

    Parameters
    ----------

    data_dir : str
        Absolute or relative path of the directory name to store bz2 files
    data_name : str
        Name of the output file in which bz2 contents will be extracted
    url : str
        URL to download data from
    data_origin_name : str
        Name of the downloaded b2 file

    Examples
    --------
    >>> get_bz2_data("data_dir", "kdda.t",
                     "https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/binary/kdda.t.bz2",
                     "kdda.t.bz2")
    """

    data_name = os.path.join(data_dir, data_name)
    data_origin_name = os.path.join(data_dir, data_origin_name)
    if not os.path.exists(data_name):
        download(url, fname=data_origin_name, dirname=data_dir, overwrite=False)
        bz_file = bz2.BZ2File(data_origin_name, 'rb')
        with open(data_name, 'wb') as fout:
            for line in bz_file:
                fout.write(line)
            bz_file.close()
        os.remove(data_origin_name) 
开发者ID:awslabs,项目名称:dynamic-training-with-apache-mxnet-on-aws,代码行数:34,代码来源:test_utils.py

示例11: make_closing

# 需要导入模块: import bz2 [as 别名]
# 或者: from bz2 import BZ2File [as 别名]
def make_closing(base, **attrs):
        """
        Add support for `with Base(attrs) as fout:` to the base class if it's missing.
        The base class' `close()` method will be called on context exit, to always close the file properly.

        This is needed for gzip.GzipFile, bz2.BZ2File etc in older Pythons (<=2.6), which otherwise
        raise "AttributeError: GzipFile instance has no attribute '__exit__'".

        """
        if not hasattr(base, '__enter__'):
            attrs['__enter__'] = lambda self: self
        if not hasattr(base, '__exit__'):
            attrs['__exit__'] = lambda self, type, value, traceback: self.close()
        return type('Closing' + base.__name__, (base, object), attrs) 
开发者ID:hankcs,项目名称:pyhanlp,代码行数:16,代码来源:util.py

示例12: smart_open

# 需要导入模块: import bz2 [as 别名]
# 或者: from bz2 import BZ2File [as 别名]
def smart_open(fname, mode='rb'):
        _, ext = os.path.splitext(fname)
        if ext == '.bz2':
            from bz2 import BZ2File
            return make_closing(BZ2File)(fname, mode)
        if ext == '.gz':
            from gzip import GzipFile
            return make_closing(GzipFile)(fname, mode)
        return open(fname, mode)


# noinspection PyUnresolvedReferences 
开发者ID:hankcs,项目名称:pyhanlp,代码行数:14,代码来源:util.py

示例13: create_temp_file

# 需要导入模块: import bz2 [as 别名]
# 或者: from bz2 import BZ2File [as 别名]
def create_temp_file(
      self, suffix='', lines=None,
      compression_type=filesystem.CompressionTypes.UNCOMPRESSED):
    """Creates a temporary file in the temporary directory.

    Args:
      suffix (str): The filename suffix of the temporary file (e.g. '.txt')
      lines (List[str]): A list of lines that will be written to the temporary
        file.
      compression_type (str): Specifies compression type of the file. Value
        should be one of ``CompressionTypes``.
    Returns:
      The name of the temporary file created.
    Raises:
      ValueError: If ``compression_type`` is unsupported.
    """
    f = tempfile.NamedTemporaryFile(delete=False,
                                    dir=self._tempdir,
                                    suffix=suffix)
    if not lines:
      return f.name
    if compression_type in (filesystem.CompressionTypes.UNCOMPRESSED,
                            filesystem.CompressionTypes.AUTO):
      f.write(''.join(lines))
    elif compression_type == filesystem.CompressionTypes.GZIP:
      with gzip.GzipFile(f.name, 'w') as gzip_file:
        gzip_file.write(''.join(lines))
    elif compression_type == filesystem.CompressionTypes.BZIP2:
      with bz2.BZ2File(f.name, 'w') as bzip_file:
        bzip_file.write(''.join(lines))
    else:
      raise ValueError('Unsupported CompressionType.')

    return f.name 
开发者ID:googlegenomics,项目名称:gcp-variant-transforms,代码行数:36,代码来源:temp_dir.py

示例14: test_compressed

# 需要导入模块: import bz2 [as 别名]
# 或者: from bz2 import BZ2File [as 别名]
def test_compressed():
    # we can read minc compressed
    content = open(MINC_EXAMPLE['fname'], 'rb').read()
    openers_exts = ((gzip.open, '.gz'), (bz2.BZ2File, '.bz2'))
    with InTemporaryDirectory():
        for opener, ext in openers_exts:
            fname = 'test.mnc' + ext
            fobj = opener(fname, 'wb')
            fobj.write(content)
            fobj.close()
            img = load(fname)
            data = img.get_data()
            assert_array_almost_equal(data.mean(), 0.60602819)
            del img 
开发者ID:ME-ICA,项目名称:me-ica,代码行数:16,代码来源:test_minc.py

示例15: allopen

# 需要导入模块: import bz2 [as 别名]
# 或者: from bz2 import BZ2File [as 别名]
def allopen(fname, *args, **kwargs):
    ''' Generic file-like object open

    If input ``fname`` already looks like a file, pass through.
    If ``fname`` ends with recognizable compressed types, use python
    libraries to open as file-like objects (read or write)
    Otherwise, use standard ``open``.
    '''
    if hasattr(fname, 'write'):
        return fname
    if args:
        mode = args[0]
    elif 'mode' in kwargs:
        mode = kwargs['mode']
    else:
        mode = 'rb'
        args = (mode,)
    if fname.endswith('.gz') or fname.endswith('.mgz'):
        if ('w' in mode and
            len(args) < 2 and
            not 'compresslevel' in kwargs):
            kwargs['compresslevel'] = default_compresslevel
        opener = gzip.open
    elif fname.endswith('.bz2'):
        if ('w' in mode and
            len(args) < 3 and
            not 'compresslevel' in kwargs):
            kwargs['compresslevel'] = default_compresslevel
        opener = bz2.BZ2File
    else:
        opener = open
    return opener(fname, *args, **kwargs) 
开发者ID:ME-ICA,项目名称:me-ica,代码行数:34,代码来源:volumeutils.py


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