diff options
Diffstat (limited to 'numpy/lib/npyio.py')
-rw-r--r-- | numpy/lib/npyio.py | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index ff5e24bef..36589ce82 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -763,7 +763,7 @@ def _getconv(dtype): elif issubclass(typ, np.floating): return floatconv elif issubclass(typ, complex): - return lambda x: complex(asstr(x)) + return lambda x: complex(asstr(x).replace('+-', '-')) elif issubclass(typ, np.bytes_): return asbytes elif issubclass(typ, np.unicode_): @@ -1109,11 +1109,16 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, nshape = list(X.shape) pos = nshape[0] nshape[0] += len(x) - X.resize(nshape) + X.resize(nshape, refcheck=False) X[pos:, ...] = x finally: if fown: fh.close() + # recursive closures have a cyclic reference to themselves, which + # requires gc to collect (gh-10620). To avoid this problem, for + # performance and PyPy friendliness, we break the cycle: + flatten_dtype_internal = None + pack_items = None if X is None: X = np.array([], dtype) @@ -1166,13 +1171,14 @@ def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', multi-format string, e.g. 'Iteration %d -- %10.5f', in which case `delimiter` is ignored. For complex `X`, the legal options for `fmt` are: - a) a single specifier, `fmt='%.4e'`, resulting in numbers formatted - like `' (%s+%sj)' % (fmt, fmt)` - b) a full string specifying every real and imaginary part, e.g. - `' %.4e %+.4ej %.4e %+.4ej %.4e %+.4ej'` for 3 columns - c) a list of specifiers, one per column - in this case, the real - and imaginary part must have separate specifiers, - e.g. `['%.3e + %.3ej', '(%.15e%+.15ej)']` for 2 columns + + * a single specifier, `fmt='%.4e'`, resulting in numbers formatted + like `' (%s+%sj)' % (fmt, fmt)` + * a full string specifying every real and imaginary part, e.g. + `' %.4e %+.4ej %.4e %+.4ej %.4e %+.4ej'` for 3 columns + * a list of specifiers, one per column - in this case, the real + and imaginary part must have separate specifiers, + e.g. `['%.3e + %.3ej', '(%.15e%+.15ej)']` for 2 columns delimiter : str, optional String or character separating columns. newline : str, optional @@ -1377,7 +1383,8 @@ def savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', for number in row: row2.append(number.real) row2.append(number.imag) - fh.write(format % tuple(row2) + newline) + s = format % tuple(row2) + newline + fh.write(s.replace('+-', '-')) else: for row in X: try: @@ -1465,9 +1472,9 @@ def fromregex(file, regexp, dtype, encoding=None): dtype = np.dtype(dtype) content = file.read() - if isinstance(content, bytes) and not isinstance(regexp, bytes): + if isinstance(content, bytes) and isinstance(regexp, np.unicode): regexp = asbytes(regexp) - elif not isinstance(content, bytes) and isinstance(regexp, bytes): + elif isinstance(content, np.unicode) and isinstance(regexp, bytes): regexp = asstr(regexp) if not hasattr(regexp, 'match'): @@ -1719,7 +1726,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, try: while not first_values: first_line = _decode_line(next(fhd), encoding) - if names is True: + if (names is True) and (comments is not None): if comments in first_line: first_line = ( ''.join(first_line.split(comments)[1:])) @@ -1733,8 +1740,9 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, # Should we take the first values as names ? if names is True: fval = first_values[0].strip() - if fval in comments: - del first_values[0] + if comments is not None: + if fval in comments: + del first_values[0] # Check the columns to use: make sure `usecols` is a list if usecols is not None: |