henbf 最近的时间轴更新
首先,我还是先来测试一下这个是个什么东西再说
2016-05-16 23:28:18 +08:00
henbf

henbf

🏢  互联网
V2EX 第 171040 号会员,加入于 2016-04-28 15:55:54 +08:00
12 G 53 S 16 B
henbf 最近回复了
@zhouyin ✅✅✅,Node.js 不适合解析 csv ,Python 牛逼
@zhouyin 这中间还要看你对 csv 的每一行进行了怎么样的处理,你用 python 只是一读一写没有任何额外的处理,相当于复制。用 Node.js ,你却把每一行转换成数组,写的时候又把数组转换成字符串,当然慢了。

const { createReadStream, createWriteStream } = require("fs");

const inputPath = "../outpy.csv";
const outputPath = "./test.txt";


const readStream = createReadStream(inputPath, { highWaterMark: 256 * 1024 });
const writeStream = createWriteStream(outputPath, { flags: "a" });

readStream.pipe(writeStream);

readStream.on("end", () => {
console.log("finished");
writeStream.end();
});

readStream.on("error", (err) => {
console.error("Error reading file:", err);
});

writeStream.on("error", (err) => {
console.error("Error writing file:", err);
});
@zhouyin 你的写的不对

const { createReadStream, createWriteStream } = require("fs");
const { parse } = require("csv-parse");

const inputPath = "../outpy.csv";
const outputPath = "./test.txt";

const readStream = createReadStream(inputPath);
const writeStream = createWriteStream(outputPath, { flags: "a" });

const parser = parse({ delimiter: ",", from_line: 2 });

readStream.pipe(parser);

parser.on("data", (row) => {
writeStream.write(row.join(",") + "\n");
});

parser.on("end", () => {
console.log("finished");
writeStream.end();
});

parser.on("error", (error) => {
console.error("CSV Parsing Error:", error);
});
喷 Node.js 之前反思一下自己是不是应该先搞清楚 I/O 和流的基本概念
看注释,这网站 Cursor 生成的吧
流量 1500G ,带宽 10MB ,就算 24 小时满带宽使用,需要 14.2 天才能用完
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2729 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 15ms · UTC 12:23 · PVG 20:23 · LAX 04:23 · JFK 07:23
Developed with CodeLauncher
♥ Do have faith in what you're doing.