Strange unicode characters when reading in file in node.js app

后端 未结 4 1507
青春惊慌失措
青春惊慌失措 2020-12-19 01:30

I am attempting to write a node app that reads in a set of files, splits them into lines, and puts the lines into an array. Pretty simple. It works on quite a few files exce

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-19 02:16

    Use the lite version of Iconv-lite

    var result= "";
    var iconv = require('iconv-lite');
    var stream = fs.createReadStream(sourcefile)
        .on("error",function(err){
            //handle error
        })
        .pipe(iconv.decodeStream('win1251'))
        .on("error",function(err){
            //handle error
        })
        .on("data",function(data){
            result += data;
        })
        .on("end",function(){
           //use result
        });
    

提交回复
热议问题