diff options
author | Ross Barnowski <rossbar@berkeley.edu> | 2022-01-11 23:02:59 -0800 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2022-01-18 14:01:01 -0600 |
commit | e15d85324a1f5641aaccafbe3cc87556e88ff0a3 (patch) | |
tree | 7d1b1485b05cdac7d2f0721feb49628db12242f3 /numpy/lib/npyio.py | |
parent | ecff02c30d774e3588535d85f9152ea6066d0b24 (diff) | |
download | numpy-e15d85324a1f5641aaccafbe3cc87556e88ff0a3.tar.gz |
Add quotechar to examples.
Diffstat (limited to 'numpy/lib/npyio.py')
-rw-r--r-- | numpy/lib/npyio.py | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 87fa2e4e2..62d00cfb9 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1241,11 +1241,31 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, Note that with the default ``encoding="bytes"``, the inputs to the converter function are latin-1 encoded byte strings. To deactivate the - implicit encoding prior to conversion, behavior use ``encoding=None`` + implicit encoding prior to conversion, use ``encoding=None`` >>> s = StringIO('10.01 31.25-\n19.22 64.31\n17.57- 63.94') >>> conv = lambda x: -float(x[:-1]) if x.endswith('-') else float(x) >>> np.loadtxt(s, converters=conv, encoding=None) + array([[ 10.01, -31.25], + [ 19.22, 64.31], + [-17.57, 63.94]]) + + Support for quoted fields is enabled with the `quotechar` parameter. + Comment and delimiter characters are ignored when they appear within a + quoted item delineated by `quotechar`: + + >>> s = StringIO('"alpha, #42", 10.0\n"beta, #64", 2.0\n') + >>> dtype = np.dtype([("label", "U12"), ("value", float)]) + >>> np.loadtxt(s, dtype=dtype, delimiter=",", quotechar='"') + array([('alpha, #42', 10.), ('beta, #64', 2.)], + dtype=[('label', '<U12'), ('value', '<f8')]) + + Two consecutive quote characters within a quoted field are treated as a + single escaped character: + + >>> s = StringIO('"Hello, my name is ""Monty""!"') + >>> np.loadtxt(s, dtype="U", delimiter=",", quotechar='"') + array('Hello, my name is "Monty"!', dtype='<U26') """ |