[Node.js] Fetch csv data and parse
We use needle
as a client on Node.js express server for fetching the data or site. https://www.npmjs.com/package/needle
The leanest and most handsome HTTP client in the Nodelands.
var needle = require('needle');
needle.get('http://www.google.com', function(error, response) {
if (!error && response.statusCode == 200)
console.log(response.body);
});
Callbacks not floating your boat? Needle got your back.
var data = {
file: '/home/johnlennon/walrus.png',
content_type: 'image/png'
};
// the callback is optional, and needle returns a `readableStream` object
// that triggers a 'done' event when the request/response process is complete.
needle
.post('https://my.server.com/foo', data, { multipart: true })
.on('readable', function() { /* eat your chunks */ })
.on('done', function(err) {
console.log('Ready-o!');
})
Then we use csv-parse
to parse the content: https://www.npmjs.com/package/csv-parse
import assert from 'assert';
import { parse } from 'csv-parse';
const records = [];
// Initialize the parser
const parser = parse({
delimiter: ':'
});
// Use the readable stream api to consume records
parser.on('readable', function(){
let record;
while ((record = parser.read()) !== null) {
records.push(record);
}
});
// Catch any error
parser.on('error', function(err){
console.error(err.message);
});
// Test that the parsed records matched the expected records
parser.on('end', function(){
assert.deepStrictEqual(
records,
[
[ 'root','x','0','0','root','/root','/bin/bash' ],
[ 'someone','x','1022','1022','','/home/someone','/bin/bash' ]
]
);
});
// Write data to the stream
parser.write("root:x:0:0:root:/root:/bin/bash\n");
parser.write("someone:x:1022:1022::/home/someone:/bin/bash\n");
// Close the readable stream
parser.end();
----
In this example, the pipe
function is used to stream the data from the remote CSV file to a local file, and the on
method is used to listen for the done
event, which is emitted once the file has finished downloading. The contents of the local file are then read using fs.readFileSync
and parsed using csv-parse
.
const needle = require('needle');
const parse = require('csv-parse');
const fs = require('fs');
needle.get('https://example.com/file.csv')
.pipe(fs.createWriteStream('file.csv'))
.on('done', function() {
// read the file once it's finished downloading
const csv = fs.readFileSync('file.csv', 'utf-8');
// parse the CSV content
const records = parse(csv, {
columns: true,
trim: true
});
// do something with the parsed data
console.log(records);
});
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2022-02-05 [RxJS] sample
2020-02-05 [Angular] Define a custom Material theme
2019-02-05 [Functional Programming ADT] Create State ADT Based Reducers (applyTo, Maybe)
2018-02-05 [Javascirpt] Developer-friendly Flow Charts with flowchart.js
2018-02-05 [TS] Class Properties Public, Private and Read Only Modifiers
2018-02-05 [Python + Unit Testing] Write Your First Python Unit Test with pytest
2018-02-05 [React] React.PureComponent