我有一个这种格式的.txt文件:
Part #368 - XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Part #369 - XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Part #370 - XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
我读了这样的文件:
var lines = fs.readFileSync('file.txt', 'utf-8')
.split('\n')
.filter(Boolean);
因此它返回文件行的数组。如何从" Part"开始获取文件的块?字符串?
var parts = _.filter(lines,function( s ) { return s.indexOf( 'Part' ) !== -1; });
像这样的东西,而不是以" Part"开头的字符串。我想要来自" Part"字符串到下一个"部分"字符串。
答案 0 :(得分:0)
这将创建一个数组行数组。
var parts = _.reduce(lines, function( result, line ) {
if (line.indexOf('Part') !== -1) result.push([]);
_.last(result).push(line);
return result;
}, []);
答案 1 :(得分:0)
根据@Brad's suggestion,这是一个从stream.Transform
扩展的类,它将文件分隔为JSON数组流:
DEBUG
使用示例:
const { Transform } = require('stream');
class Delimited extends Transform {
constructor({ delimiter = /\r?\n/g, encoding = 'utf8' } = {}) {
super();
// initialize internal values
this._delimiter = delimiter instanceof RegExp ? delimiter : new RegExp(delimiter, 'g');
this._encoding = encoding;
this._buffer = '';
this._first = true;
}
_transform(chunk, encoding, callback) {
// convert input encoding into output encoding
// and append to internal buffer
if (encoding === 'buffer') {
this._buffer += chunk.toString(this._encoding);
} else if (encoding === this._encoding) {
this._buffer += chunk;
} else {
this._buffer += Buffer.from(chunk, encoding).toString(this._encoding);
}
let partialJSON = '';
// check if delimiter is found
if (this._delimiter.test(this._buffer)) {
// split internal buffer by delimiter
let sections = this._buffer.split(this._delimiter);
// put possibly incomplete section from array back into internal buffer
this._buffer = sections.pop();
// add each section to partial json array
sections.forEach(section => {
partialJSON += `${this._first ? '[' : ','}${JSON.stringify(section)}`;
this._first = false;
});
}
// push partial json array to readable stream
callback(null, partialJSON);
}
_flush(callback) {
// add remaining buffer as last section to json array
callback(null, `${this._first ? '[' : ','}${JSON.stringify(this._buffer)}]`);
}
}
或者,如果您不想将JSON转移到另一个文件,进程或作为客户端响应,则可以通过将输出流设置为const fs = require('fs');
let stream = fs.createReadStream('file.txt', 'utf8');
let transform = new Delimited({ delimiter: /\n\n(?=Part #\d)/g });
let json = '';
transform.on('data', (chunk) => json += chunk);
transform.on('end', () => console.log(JSON.parse(json)));
stream.pipe(transform);
来将每个部分作为块发送:
objectMode: true
使用示例:
const { Transform } = require('stream');
class Delimited extends Transform {
constructor(delimiter = /\r?\n/g) {
super({ objectMode: true });
// initialize internal values
this._delimiter = delimiter instanceof RegExp ? delimiter : new RegExp(delimiter, 'g');
this._encoding = 'utf8';
this._buffer = '';
this._first = true;
}
_transform(chunk, encoding, callback) {
// convert input encoding into output encoding
// and append to internal buffer
if (encoding === 'buffer') {
this._buffer += chunk.toString(this._encoding);
} else if (encoding === this._encoding) {
this._buffer += chunk;
} else {
this._buffer += Buffer.from(chunk, encoding).toString(this._encoding);
}
if (this._delimiter.test(this._buffer)) {
// split internal buffer by delimiter
let sections = this._buffer.split(this._delimiter);
// put possibly incomplete section from array back into internal buffer
this._buffer = sections.pop();
// push each section to readable stream in object mode
sections.forEach(this.push, this);
}
callback();
}
_flush(callback) {
// push remaining buffer to readable stream
callback(null, this._buffer);
}
}