最终目标是输入一个庞大的txt文件并替换csv文件中给出的所有单词。将在一个新的csv文件旁边创建一个新的txt文件,该文件将计算每个单词的出现次数。
我遇到的问题是将地图数据写入新的csv文件。我得到的数据是不确定的,但是如何将其更改为可读的?
无论我将哪种map.thingie放在data.push字段中,都会出现错误。
var fs = require('fs');
var inputFile='OneLetter.csv';
var parse = require('csv-parse');
var map1 = new Map();
var iterator = map1[Symbol.iterator]();
//This part creates a stream, essentially reads it and creates a list
var parser = parse({delimiter: ';'}, function (err, data) {
data.forEach(function(line) {
// create line object out of parsed fields
var list = map1.set(line)
});
for (let item of iterator) {
console.log(item)
};
});
fs.createReadStream(inputFile).pipe(parser);
//This program writes a CSV file containing data in the data1, data2... fields
let data = [];
let columns = {
Column1: '# times',
Column2: 'Original',
Column3: 'Short'
};
data.push(['data1',
'data2',
'data3'
]);
var stringify = require('csv-stringify');
stringify(data ,{ delimiter: ';', header: true, columns: columns }, (err, output) => {
if (err) throw err;
fs.writeFile('TEST.csv', output, (err) => {
if (err) throw err;
console.log('TEST.csv saved.');
});
});
Working Result:
[ [ 'A', 'A' ], undefined ]
[ [ 'B', 'BE' ], undefined ]
[ [ 'C', 'SEE' ], undefined ]
[ [ 'D', 'IN THE' ], undefined ]
[ [ 'E', 'HE' ], undefined ]
[ [ 'F', 'OF THE' ], undefined ]
[ [ 'G', 'FROM THE' ], undefined ]
[ [ 'H', 'HAS' ], undefined ]
[ [ 'I', 'I' ], undefined ]
[ [ 'J', 'BY WHICH' ], undefined ]
[ [ 'K', 'OUT OF THE' ], undefined ]
[ [ 'L', 'ALL' ], undefined ]
[ [ 'M', 'MORE' ], undefined ]
[ [ 'N', 'NOT' ], undefined ]
[ [ 'O', 'OF' ], undefined ]
[ [ 'P', 'PER' ], undefined ]
[ [ 'Q', 'ON THE' ], undefined ]
[ [ 'R', 'ARE' ], undefined ]
[ [ 'S', 'SEPERATOR' ], undefined ]
[ [ 'T', 'THE' ], undefined ]
[ [ 'U', 'YOU' ], undefined ]
[ [ 'V', 'OF WHICH' ], undefined ]
[ [ 'W', 'WITH' ], undefined ]
[ [ 'X', 'IN WHICH' ], undefined ]
[ [ 'Y', 'YES' ], undefined ]
[ [ 'Z', 'FROM WHICH' ], undefined ]
TEST.csv saved.
非工作结果:
Error: Invalid Record: expect an array or an object, got undefined
at Stringifier._transform (C:\Program Files\nodejs\node_modules\csv-stringify\lib\index.
答案 0 :(得分:0)
我似乎没有看到根据文档在parse
方法中进行转换的选项-您需要尝试使用transform使其以您想要的方式工作-因此创建一个并像这样通过它:
const transformer = transform(function(data){
return replaceEntries(data); // here you call the replacement
});
fs.createReadStream(textFile).pipe(parser).pipe(transformer).pipe(stringify);
需要先准备好变压器
根据您的情况判断,似乎没有记录传递转换函数,并且错误可能来自此。此外,您的代码似乎并没有执行您想要达到的目标,更像是尝试创建新的CSV。
我认为您可能已经从一个奇怪的角度开始了任务,所以我建议您从头开始重做-您需要编写一个程序,按以下顺序执行以下操作:
我还建议使用我创建的scramjet框架,这可以使代码更容易理解。
const {StringStream} = require("scramjet");
StringStream
// here we read the file
.from(fs.createReadStream('massive.txt'))
.use(async stream => {
// here we create replacements
const replacements = await (
StringStream.from(fs.createReadStream('massive.txt'))
.csvParse({header: false})
// the line below will make sure we're replacing whole words
.map(([_from, _to]) => ([new RegExp(`\b${_from}\b`, "g"), _to])
.toArray()
);
// here we map all the items in the stream and iterate over all the replacements
return stream.map(
chunk => replacements.reduce((s, [_from, _to]) => s.replace(_from, _to), chunk)
);
})
// and here we push the replaced stream to the output file
.pipe(fs.createWriteStream('massive-file-out.txt');
请检查以.map(([_from, _to]) =>
开头的行。我假设您只想在整个单词中替换文本,所以更多R U ready -> Are you ready
,而不是Robert -> Areobert
-这是此行的目的。