所以我有一个巨大的文本文件,如下所示:
e10_04txyscelsxeu4j63dx49b7nh3dzsn_q33_fdgskfdn_q53
e2_05txyscelsxeu4j63dx49b7nh3dzsn_q11_fdgskfdn_q13
e9_01_1txyscelsxeu4j63dx49b7nh3dzsn_q06_fdgskfdn_q42
e10_23txyscelsxeu4j63dx49b7nh3dzsn_q04_fdgskfdn_q41
有这些随机字符串的列表。我试图迭代每一行,抓取前 10 个字母/数字并将它们吐出到某个地方,这样我就可以用它们做一些事情。
目前我正在迭代每个字母,如下所示:
const fs = require('fs');
fs.readFile('myData', 'utf8', (err, data) => {
for (var i = 0, j = data.length; i < j; i++) {
if (i == 10) {
console.log('test');
} else {
}
}
});
我能做我想做的事吗?
谢谢!
最佳答案
您可以使用readline
一次读取文件一行:
// this creates a read stream
var reader = require('readline').createInterface({
input: require('fs').createReadStream('file.in')
});
// then here you would be able to manipulate each line:
reader.on('line', function (line) {
console.log('The current line is: ', line);
});
关于javascript - 迭代nodejs字符串行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37139618/