Javascript node.js:将文本文件读入数组。(每一行都是数组中的一个项目。)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6831918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
node.js: read a text file into an array. (Each line an item in the array.)
提问by chacko
I would like to read a very, very large file into a JavaScript array in node.js.
我想将一个非常非常大的文件读入 node.js 中的 JavaScript 数组。
So, if the file is like this:
所以,如果文件是这样的:
first line
two
three
...
...
I would have the array:
我会有数组:
['first line','two','three', ... , ... ]
The function would look like this:
该函数如下所示:
var array = load(filename);
Therefore the idea of loading it all as a string and then splitting it is not acceptable.
因此,将其全部加载为字符串然后将其拆分的想法是不可接受的。
采纳答案by mtomis
If you can fit the final data into an array then wouldn't you also be able to fit it in a string and split it, as has been suggested? In any case if you would like to process the file one line at a time you can also try something like this:
如果您可以将最终数据放入一个数组中,那么您是否也可以将它放入一个字符串中并将其拆分,正如所建议的那样?在任何情况下,如果您想一次处理一行文件,您也可以尝试以下操作:
var fs = require('fs');
function readLines(input, func) {
var remaining = '';
input.on('data', function(data) {
remaining += data;
var index = remaining.indexOf('\n');
while (index > -1) {
var line = remaining.substring(0, index);
remaining = remaining.substring(index + 1);
func(line);
index = remaining.indexOf('\n');
}
});
input.on('end', function() {
if (remaining.length > 0) {
func(remaining);
}
});
}
function func(data) {
console.log('Line: ' + data);
}
var input = fs.createReadStream('lines.txt');
readLines(input, func);
EDIT:(in response to comment by phopkins) I think (at least in newer versions) substring does not copy data but creates a special SlicedString object (from a quick glance at the v8 source code). In any case here is a modification that avoids the mentioned substring (tested on a file several megabytes worth of "All work and no play makes Hyman a dull boy"):
编辑:(响应phopkins 的评论)我认为(至少在较新版本中) substring 不会复制数据,而是创建一个特殊的 SlicedString 对象(快速浏览 v8 源代码)。在任何情况下,这里都有一个避免提到的子字符串的修改(在一个价值几兆字节的“所有工作和不玩耍使Hyman成为一个无趣的男孩”的文件上进行测试):
function readLines(input, func) {
var remaining = '';
input.on('data', function(data) {
remaining += data;
var index = remaining.indexOf('\n');
var last = 0;
while (index > -1) {
var line = remaining.substring(last, index);
last = index + 1;
func(line);
index = remaining.indexOf('\n', last);
}
remaining = remaining.substring(last);
});
input.on('end', function() {
if (remaining.length > 0) {
func(remaining);
}
});
}
回答by Finbarr
Synchronous:
同步:
var fs = require('fs');
var array = fs.readFileSync('file.txt').toString().split("\n");
for(i in array) {
console.log(array[i]);
}
Asynchronous:
异步:
var fs = require('fs');
fs.readFile('file.txt', function(err, data) {
if(err) throw err;
var array = data.toString().split("\n");
for(i in array) {
console.log(array[i]);
}
});
回答by zswang
Using the Node.js readline module.
使用 Node.js readline 模块。
var fs = require('fs');
var readline = require('readline');
var filename = process.argv[2];
readline.createInterface({
input: fs.createReadStream(filename),
terminal: false
}).on('line', function(line) {
console.log('Line: ' + line);
});
回答by hojin
js:
js:
var array = fs.readFileSync('file.txt', 'utf8').split('\n');
ts:
ts:
var array = fs.readFileSync('file.txt', 'utf8').toString().split('\n');
回答by Blair Anderson
use readline (documentation). here's an example reading a css file, parsing for icons and writing them to json
使用 readline (文档)。这是一个读取 css 文件、解析图标并将它们写入 json 的示例
var results = [];
var rl = require('readline').createInterface({
input: require('fs').createReadStream('./assets/stylesheets/_icons.scss')
});
// for every new line, if it matches the regex, add it to an array
// this is ugly regex :)
rl.on('line', function (line) {
var re = /\.icon-icon.*:/;
var match;
if ((match = re.exec(line)) !== null) {
results.push(match[0].replace(".",'').replace(":",''));
}
});
// readline emits a close event when the file is read.
rl.on('close', function(){
var outputFilename = './icons.json';
fs.writeFile(outputFilename, JSON.stringify(results, null, 2), function(err) {
if(err) {
console.log(err);
} else {
console.log("JSON saved to " + outputFilename);
}
});
});
回答by Abdennour TOUMI
file.lines
with JFile package
file.lines
使用JFile 包
Pseudo
伪
var JFile=require('jfile');
var myF=new JFile("./data.txt");
myF.lines // ["first line","second line"] ....
Don't forget before :
之前不要忘记:
npm install jfile --save
回答by Gabriel Llamas
With a BufferedReader, but the function should be asynchronous:
使用BufferedReader,但函数应该是异步的:
var load = function (file, cb){
var lines = [];
new BufferedReader (file, { encoding: "utf8" })
.on ("error", function (error){
cb (error, null);
})
.on ("line", function (line){
lines.push (line);
})
.on ("end", function (){
cb (null, lines);
})
.read ();
};
load ("file", function (error, lines){
if (error) return console.log (error);
console.log (lines);
});
回答by HernanFila
i just want to add @finbarr great answer, a little fix in the asynchronous example:
我只想添加@finbarr 很好的答案,在异步示例中稍作修正:
Asynchronous:
异步:
var fs = require('fs');
fs.readFile('file.txt', function(err, data) {
if(err) throw err;
var array = data.toString().split("\n");
for(i in array) {
console.log(array[i]);
}
done();
});
@MadPhysicist, done() is what releases the async. call.
@MadPhysicist,done() 是释放异步的。称呼。
回答by oferei
This is a variation on the answer above by @mtomis.
这是@mtomis 上面答案的一个变体。
It creates a stream of lines. It emits 'data' and 'end' events, allowing you to handle the end of the stream.
它创建了一条线。它发出 'data' 和 'end' 事件,允许您处理流的结尾。
var events = require('events');
var LineStream = function (input) {
var remaining = '';
input.on('data', function (data) {
remaining += data;
var index = remaining.indexOf('\n');
var last = 0;
while (index > -1) {
var line = remaining.substring(last, index);
last = index + 1;
this.emit('data', line);
index = remaining.indexOf('\n', last);
}
remaining = remaining.substring(last);
}.bind(this));
input.on('end', function() {
if (remaining.length > 0) {
this.emit('data', remaining);
}
this.emit('end');
}.bind(this));
}
LineStream.prototype = new events.EventEmitter;
Use it as a wrapper:
将其用作包装器:
var lineInput = new LineStream(input);
lineInput.on('data', function (line) {
// handle line
});
lineInput.on('end', function() {
// wrap it up
});
回答by Antoni
I had the same problem, and I have solved it with the module line-by-line
我遇到了同样的问题,我已经用模块逐行解决了
https://www.npmjs.com/package/line-by-line
https://www.npmjs.com/package/line-by-line
At least for me works like a charm, both in synchronous and asynchronous mode.
至少对我来说,无论是在同步模式还是异步模式下,它都像一个魅力。
Also, the problem with lines terminating not terminating \n can be solved with the option:
此外,可以使用以下选项解决行终止而不终止 \n 的问题:
{ encoding: 'utf8', skipEmptyLines: false }
Synchronous processing of lines:
线路的同步处理:
var LineByLineReader = require('line-by-line'),
lr = new LineByLineReader('big_file.txt');
lr.on('error', function (err) {
// 'err' contains error object
});
lr.on('line', function (line) {
// 'line' contains the current line without the trailing newline character.
});
lr.on('end', function () {
// All lines are read, file is closed now.
});