diff --git a/NEWS.md b/NEWS.md index f74eed63a8..4a2b099cba 100644 --- a/NEWS.md +++ b/NEWS.md @@ -96,6 +96,8 @@ 21. GForce grouping is again faster when running single-threaded, e.g. after `setDTthreads(1)`, [#3330](https://github.com/Rdatatable/data.table/issues/3330). The batching used to parallelize `gsum()` and `gmean()` is skipped when only one thread is available. Also, `mean()` of an `integer` or `logical` column by group no longer coerces the whole column to `double` first, which made it slower than in v1.12.0 regardless of the number of threads. Thanks @jangorecki for the benchmarks, @mattdowle for the report, and @ben-schwen for the fix. +22. `fread()` parses quoted numbers with leading or trailing whitespace as numeric rather than character, matching `read.table()` behavior, [#7946](https://github.com/Rdatatable/data.table/issues/7946). Thanks @CCIAABZ for the report and @loganb7869, @lds295, @DiamondCat106 for the PR. + ### Notes 1. {data.table} now depends on R 3.5.0 (2018). diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 3a446a8136..b295e9ee3f 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -22014,3 +22014,7 @@ test(2388.24, nafill(c("a", NA, NA, "z"), type="nocb", limit=1), c("a", NA, "z", x = seq(as.IDate("1999-01-01"), as.IDate("2027-12-31"), by="day") test(2389.1, isoweek(x), as.integer(format(x, "%V"))) test(2389.2, isoyear(x), as.integer(format(x, "%G"))) +test(2390.1, fread('"a";"b"\n"x";" 123"\n"y";" 456"\n"z";" 789"', sep=";"), data.table(a=c("x", "y", "z"), b=c(123L, 456L, 789L))) +test(2390.2, fread('"a";"b"\n"x";"123 "\n"y";"456 "\n"z";"789 "', sep=";"), data.table(a=c("x", "y", "z"), b=c(123L, 456L, 789L))) +test(2390.3, fread('"a";"b"\n"x";" 12.5 "\n"y";" 45.6 "', sep=";"), data.table(a=c("x", "y"), b=c(12.5, 45.6))) +test(2390.4, fread('"a";"b"\n" abc ";123\n" def ";456', sep=";"), data.table(a=c(" abc ", " def "), b=c(123L, 456L))) \ No newline at end of file diff --git a/src/fread.c b/src/fread.c index d9aab52da5..8f047838d6 100644 --- a/src/fread.c +++ b/src/fread.c @@ -1344,7 +1344,9 @@ static int detect_types(const char **pch, int ncol, bool *bumped) } if (*ch == quote && quote) { // && quote to exclude quote='\0' (user passed quote="") ch++; + skip_white(&ch); fun[tmpType[field]](&fctx); + skip_white(&ch); if (*ch == quote) { ch++; skip_white(&ch); @@ -2685,10 +2687,12 @@ int freadMain(freadMainArgs _args) if (!end_of_field(tch)) tch = afterSpace; // else it is the field_end, we're on closing sep|eol and we'll let processor write appropriate NA as if field was empty if (*tch == quote && quote) { quoted = true; tch++; } } // else Field() handles NA inside it unlike other processors e.g. ,, is interpreted as "" or NA depending on option read inside Field() + if (quoted) skip_white(&tch); fun[IGNORE_BUMP(thisType)](&fctx); bool typeBump = false; if (quoted) { // quoted was only set to true with '&& quote' above (=> quote!='\0' now) + skip_white(&tch); if (*tch == quote) tch++; else typeBump = true; }