From 19108f28764376a343b3c5b8768dc8c9b8512bcf Mon Sep 17 00:00:00 2001 From: jangorecki Date: Wed, 18 Nov 2026 21:16:47 +0100 Subject: [PATCH 1/4] handle zsv data nicely --- R/fread.R | 15 +++++++++++++++ inst/tests/tests.Rraw | 8 ++++++++ 2 files changed, 23 insertions(+) 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 3a446a8136..fcd0e7557f 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -22014,3 +22014,11 @@ 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"))) + +# zsv format handling # as of zsv@094745ac094f7a17bf95fc5f0a29580ad24a1a81 +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(2390.01, names(fread(zsv)), c("id","year","city","amount")) From b3b1eb3bd13c5b5bed9f2f1624f3650c3f050251 Mon Sep 17 00:00:00 2001 From: jangorecki Date: Fri, 20 Nov 2026 22:05:29 +0100 Subject: [PATCH 2/4] check that other marker does not trigger trim --- inst/tests/tests.Rraw | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index fcd0e7557f..e8c145cc86 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -22015,10 +22015,16 @@ 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"))) -# zsv format handling # as of zsv@094745ac094f7a17bf95fc5f0a29580ad24a1a81 +# zsv format handling 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(2390.01, names(fread(zsv)), c("id","year","city","amount")) +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(2390.02, names(fread(csv)), c("id~u8-index~0~csv","year~u16-partition{2024@0|2025@30|2026@45}","city~str","amount~f64")) From 8f8078b38038efdda4686f4b5d8ba7faaf5654c0 Mon Sep 17 00:00:00 2001 From: jangorecki Date: Fri, 20 Nov 2026 22:08:52 +0100 Subject: [PATCH 3/4] news entry --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index f74eed63a8..f76376b9ca 100644 --- a/NEWS.md +++ b/NEWS.md @@ -118,6 +118,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) From af87472fda7534688f1e6fe77816304056ba3601 Mon Sep 17 00:00:00 2001 From: jangorecki Date: Thu, 3 Dec 2026 22:04:46 +0100 Subject: [PATCH 4/4] add comments per review --- inst/tests/tests.Rraw | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index a835c1a2bc..149557899a 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -22020,13 +22020,14 @@ test(2390.2, fread('"a";"b"\n"x";"123 "\n"y";"456 "\n"z";"789 "', sep=";"), data 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))) -# zsv format handling +# 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,