From 6d474f2dfe5e4b7ea2a6e7423a638316fa186227 Mon Sep 17 00:00:00 2001 From: dmbelov Date: Sun, 1 Jan 2023 07:38:57 -0500 Subject: BUG: np.loadtxt cannot load text file with quoted fields separated by whitespace (#22906) Fix issue with `delimiter=None` and quote character not working properly (not using whitespace delimiter mode). Closes gh-22899 --- numpy/lib/tests/test_loadtxt.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'numpy/lib/tests') diff --git a/numpy/lib/tests/test_loadtxt.py b/numpy/lib/tests/test_loadtxt.py index 819a8dda4..8a5b044b8 100644 --- a/numpy/lib/tests/test_loadtxt.py +++ b/numpy/lib/tests/test_loadtxt.py @@ -534,6 +534,20 @@ def test_quoted_field(q): assert_array_equal(res, expected) +@pytest.mark.parametrize("q", ('"', "'", "`")) +def test_quoted_field_with_whitepace_delimiter(q): + txt = StringIO( + f"{q}alpha, x{q} 2.5\n{q}beta, y{q} 4.5\n{q}gamma, z{q} 5.0\n" + ) + dtype = np.dtype([('f0', 'U8'), ('f1', np.float64)]) + expected = np.array( + [("alpha, x", 2.5), ("beta, y", 4.5), ("gamma, z", 5.0)], dtype=dtype + ) + + res = np.loadtxt(txt, dtype=dtype, delimiter=None, quotechar=q) + assert_array_equal(res, expected) + + def test_quote_support_default(): """Support for quoted fields is disabled by default.""" txt = StringIO('"lat,long", 45, 30\n') -- cgit v1.2.1