本文整理匯總了Python中numpy.lib._iotools._is_string_like方法的典型用法代碼示例。如果您正苦於以下問題:Python _iotools._is_string_like方法的具體用法?Python _iotools._is_string_like怎麽用?Python _iotools._is_string_like使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy.lib._iotools
的用法示例。
在下文中一共展示了_iotools._is_string_like方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_file_obj
# 需要導入模塊: from numpy.lib import _iotools [as 別名]
# 或者: from numpy.lib._iotools import _is_string_like [as 別名]
def get_file_obj(fname, mode='r', encoding=None):
"""
Light wrapper to handle strings and let files (anything else) pass through.
It also handle '.gz' files.
Parameters
==========
fname: string or file-like object
File to open / forward
mode: string
Argument passed to the 'open' or 'gzip.open' function
encoding: string
For Python 3 only, specify the encoding of the file
Returns
=======
A file-like object that is always a context-manager. If the `fname` was already a file-like object,
the returned context manager *will not close the file*.
"""
if _is_string_like(fname):
return _open(fname, mode, encoding)
try:
# Make sure the object has the write methods
if 'r' in mode:
fname.read
if 'w' in mode or 'a' in mode:
fname.write
except AttributeError:
raise ValueError('fname must be a string or a file-like object')
return EmptyContextManager(fname)
示例2: drop_fields
# 需要導入模塊: from numpy.lib import _iotools [as 別名]
# 或者: from numpy.lib._iotools import _is_string_like [as 別名]
def drop_fields(base, drop_names, usemask=True, asrecarray=False):
"""
Return a new array with fields in `drop_names` dropped.
Nested fields are supported.
Parameters
----------
base : array
Input array
drop_names : string or sequence
String or sequence of strings corresponding to the names of the fields
to drop.
usemask : {False, True}, optional
Whether to return a masked array or not.
asrecarray : string or sequence
Whether to return a recarray or a mrecarray (`asrecarray=True`) or
a plain ndarray or masked array with flexible dtype (`asrecarray=False`)
Examples
--------
>>> from numpy.lib import recfunctions as rfn
>>> a = np.array([(1, (2, 3.0)), (4, (5, 6.0))],
... dtype=[('a', int), ('b', [('ba', float), ('bb', int)])])
>>> rfn.drop_fields(a, 'a')
array([((2.0, 3),), ((5.0, 6),)],
dtype=[('b', [('ba', '<f8'), ('bb', '<i4')])])
>>> rfn.drop_fields(a, 'ba')
array([(1, (3,)), (4, (6,))],
dtype=[('a', '<i4'), ('b', [('bb', '<i4')])])
>>> rfn.drop_fields(a, ['ba', 'bb'])
array([(1,), (4,)],
dtype=[('a', '<i4')])
"""
if _is_string_like(drop_names):
drop_names = [drop_names, ]
else:
drop_names = set(drop_names)
#
def _drop_descr(ndtype, drop_names):
names = ndtype.names
newdtype = []
for name in names:
current = ndtype[name]
if name in drop_names:
continue
if current.names:
descr = _drop_descr(current, drop_names)
if descr:
newdtype.append((name, descr))
else:
newdtype.append((name, current))
return newdtype
#
newdtype = _drop_descr(base.dtype, drop_names)
if not newdtype:
return None
#
output = np.empty(base.shape, dtype=newdtype)
output = recursive_fill_fields(base, output)
return _fix_output(output, usemask=usemask, asrecarray=asrecarray)