Published
Edited
Jun 9, 2020
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
not_so_good = d3.csvParse(text)
Insert cell
table(not_so_good)
Insert cell
Insert cell
a_bit_better = d3.csvParse(text, row =>
String(Object.entries(row)[0][1]).match(/^#/) ? null : row
)
Insert cell
table(a_bit_better)
Insert cell
Insert cell
rows = {
let comments = "";
const data = d3.csvParse(text, row =>
comments || String(Object.entries(row)[0][1]).match(/^#/)
? ((comments = comments.concat(
Object.values(row)
.filter(d => d)
.join(",") + "\n"
)),
null)
: row
);

return Object.assign(data, { comments });
}
Insert cell
Insert cell
Insert cell
Insert cell
table(d3.csvParse(text2)) // of course it cannot work
Insert cell
Insert cell
clean = {
const rows = d3.csvParseRows(text2),
comments = rows
.splice(0, 2) // <= 2 is the number of lines to consider as comments
.flat()
.join(" "),
columns = rows.splice(0, 1).flat();
return Object.assign(
rows.map(row =>
Object.fromEntries(columns.map((name, i) => [name, row[i]]))
),
{ columns, comments }
);
}
Insert cell
Insert cell
Insert cell
d3 = require("d3-dsv@1")
Insert cell
import { table } from "@tmcw/tables/2"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more