diff --git a/NEWS.md b/NEWS.md index 4a2b099cba..2bc76fdb32 100644 --- a/NEWS.md +++ b/NEWS.md @@ -120,6 +120,8 @@ 10. `fwrite()` returns a clearer error message when `na = data.frame()` is used, [#7866](https://github.com/Rdatatable/data.table/issues/7866). Thanks @mcol for the report and the fix. +11. `fread()` will now detect and automatically trim [zsv](https://codeberg.org/jangorecki/zsv) metadata embedded in column names of a csv file. [#7963](https://github.com/Rdatatable/data.table/pull/7963) + ## data.table [v1.18.6.1](https://github.com/Rdatatable/data.table/milestone/47) (22 August 2026) diff --git a/R/fread.R b/R/fread.R index 4aa6a01ed0..9e66e19d8a 100644 --- a/R/fread.R +++ b/R/fread.R @@ -370,6 +370,21 @@ yaml=FALSE, tmpdir=tempdir(), tz="UTC") } else { setattr(ans, "class", "data.frame") } + # detect zsv headers + zsv = isTRUE(base::endsWith(names(ans)[1L], "~zsv")) + if (zsv) { + truecolnames = function(x) { + pos = regexpr("~", x, fixed = TRUE) + i = pos > 0L + if (!sum(i)) internal_error("truecolnames should be called only when ~zsv marker was detected in first column name") # nocov + ans = x + ans[i] = substr(x[i], 1L, pos[i] - 1L) + ans + } + if (verbose) + catf('Detected zsv header format: trim zsv to csv\n') + setnames(ans, truecolnames(names(ans))) + } # #1027, make.unique -> make.names as spotted by @DavidArenberg if (check.names) { setattr(ans, 'names', make.names(names(ans), unique=TRUE)) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index b295e9ee3f..149557899a 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -22014,7 +22014,23 @@ 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 +test(2390.4, fread('"a";"b"\n" abc ";123\n" def ";456', sep=";"), data.table(a=c(" abc ", " def "), b=c(123L, 456L))) + +# zsv format handling # detects ~zsv marker +fwrite(fread(text = "id~u8-index~0~zsv,year~u16-partition{2024@0|2025@30|2026@45},city~str,amount~f64 +1,2024,Bern,2.5 +2,2024,Basel, +4,2025,Zug,1.5 +5,2026,Bern,3"), zsv<-tempfile(fileext=".zsv")) +test(2391.01, names(fread(zsv)), c("id","year","city","amount")) +# zsv format handling # ~csv marker is ignored +fwrite(fread(text = "id~u8-index~0~csv,year~u16-partition{2024@0|2025@30|2026@45},city~str,amount~f64 +1,2024,Bern,2.5 +2,2024,Basel, +4,2025,Zug,1.5 +5,2026,Bern,3"), csv<-tempfile(fileext=".csv")) +test(2391.02, names(fread(csv)), c("id~u8-index~0~csv","year~u16-partition{2024@0|2025@30|2026@45}","city~str","amount~f64"))