4

我正在尝试使用 d3 读取 tsv 数据。但是,我的数据有如下注释行

#Comment line
@Different comment line
x    y
1    2
4    2
5    1

是否可以让 d3 忽略这些行?

谢谢

4

1 回答 1

5

D3 没有内置的方法来忽略注释行。您最简单的选择是在使用 D3 解析之前对文件进行预处理:

d3.text(url, 'text/csv', function(csv) {
    // remove comment rows with regex - not fully tested, but should work
    csv = csv.replace(/^[#@][^\r\n]+[\r\n]+/mg, '');
    var data = d3.csv.parse(csv);

    // ... now the rest of your code ...
});
于 2012-11-18T05:22:21.740 回答