本文整理汇总了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
示例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")
示例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)
示例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)
示例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
示例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'])
示例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.
示例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)
示例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)
示例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)
示例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)
示例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
示例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
示例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
示例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)