Javascript CSV解析器文档
我已经使用FastCSV作为我的转换器库有一段时间了。当客户端实际尝试上载实际包含“;”的csv文件时,出现了一个问题作为分隔符,而不是默认的“,”。NPM文档明确指出,所有方法都应该接受一个“选项”,但不理解为什么一个对象不能实际切换这些标志。当然,我可以随时进入源js文件并手动更改分隔符,但我真的很想理解此文档,因为它是我作为开发人员成长过程中的一部分,但我仍然无法掌握如何在代码解析时实际使用这些选项分隔符。如果你们中没有人能理解它,也许你们对javascript上的csv解析器有一些建议?也许手动脚本更通用、更有用 npm上fast csv的文档样本:Javascript CSV解析器文档,javascript,node.js,csv,npm,fs,Javascript,Node.js,Csv,Npm,Fs,我已经使用FastCSV作为我的转换器库有一段时间了。当客户端实际尝试上载实际包含“;”的csv文件时,出现了一个问题作为分隔符,而不是默认的“,”。NPM文档明确指出,所有方法都应该接受一个“选项”,但不理解为什么一个对象不能实际切换这些标志。当然,我可以随时进入源js文件并手动更改分隔符,但我真的很想理解此文档,因为它是我作为开发人员成长过程中的一部分,但我仍然无法掌握如何在代码解析时实际使用这些选项分隔符。如果你们中没有人能理解它,也许你们对javascript上的csv解析器有一些建议?
All methods accept the following options
objectMode=true: Ensure that data events have an object emitted rather than the stringified version set to false to have a stringified buffer.
headers=false: Set to true if you expect the first line of your CSV to contain headers, alternatly you can specify an array of headers to use. You can also specify a sparse array to omit some of the columns.
ignoreEmpty=false: If you wish to ignore empty rows.
discardUnmappedColumns=false: If you want to discard columns that do not map to a header.
strictColumnHandling=false: If you want to consider empty lines/lines with too few fields as errors - Only to be used with headers=true
renameHeaders=false: If you want the first line of the file to be removed and replaced by the one provided in the headers option - Only to be used with headers=[String]
delimiter=',': If your data uses an alternate delimiter such as ; or \t.
此外,下面是一个示例代码,说明了它是如何工作的,以及我是如何将其用于管道的:
var stream = fs.createReadStream("my.csv");
var csvStream = csv()
.on("data", function(data){
console.log(data);
})
.on("end", function(){
console.log("done");
});
stream.pipe(csvStream);
//or
var csvStream = csv
.parse()
.on("data", function(data){
console.log(data);
})
.on("end", function(){
console.log("done");
});
stream.pipe(csvStream);
PS:我试过问它在哪里发布的包,但没有答复
NPM文档明确指出,所有方法都应该接受
一个“选项”不理解为什么一个对象不能实际切换
这些旗帜
引用的文本基本上意味着所有方法都接受所谓的options对象作为其最后一个参数。可以通过在该对象中设置相应字段来指定备用分隔符
但我真的很想理解这个文档,因为它的全部
作为开发人员,我成长的一部分
我强烈建议,如果您觉得文档中没有明确解释某些内容,请查看。实际上,对于您描述的确切场景,有一个测试用例:
it.should("support semicolon delimiters", function (next) {
var actual = [];
csv
.fromPath(path.resolve(__dirname, "./assets/test16.txt"), {headers: true, delimiter: ";"})
.on("data", function (data) {
actual.push(data);
})
.on("error", next)
.on("end", function (count) {
assert.deepEqual(actual, expected14);
assert.equal(count, actual.length);
next();
});
});
非常感谢你的时间和建议,像你这样的人让这个地方变得很棒!