Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 15 additions & 0 deletions R/fread.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
14 changes: 14 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -22014,3 +22014,17 @@ 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
fwrite(fread(text = "id~u8-index~0~zsv,year~u16-partition{2024@0|2025@30|2026@45},city~str,amount~f64
Comment thread
jangorecki marked this conversation as resolved.
Resolved
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"))
Comment on lines +22018 to +22029

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 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"))
# zsv format handling
zsv.dt = 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")
fwrite(zsv.dt, zsv<-tempfile(fileext=".zsv"))
test(2390.01, names(fread(zsv)), c("id","year","city","amount"))
fwrite(zsv.dt, csv<-tempfile(fileext=".csv"))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change suggested does not match original code, where first case has ~zsv marker vs second ~csv text, which is not proper format marker so is recognized as ordinary column name

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I did not notice that, maybe add a comment to emphasize that difference for future readers?

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"))
Loading