简体   繁体   English

如何从JavaScript中的.csv文件读取特殊字符

[英]How to read special characters from .csv files in JavaScript

I want to read .csv files which contains special characters (polish language). 我想读取包含特殊字符(波兰语)的.csv文件。

I'm using ExcelJs to read .csv: 我正在使用ExcelJs读取.csv:

    var workbook = new Excel.Workbook();
    workbook.csv.readFile(uploadsPath + "/" + filename, {delimiter: ';'})
        .then(function (worksheet) {
            var worksheet = workbook.getWorksheet(1);

            console.log(worksheet.getRow(3).getCell(7).value);
        });
}

With this code I'm getting "Wroc aw" instead of "Wrocław". 使用此代码,我得到的是“弗罗茨瓦夫”而不是“弗罗茨瓦夫”。

I tried using encoding: 我尝试使用编码:

    var workbook = new Excel.Workbook();
    workbook.csv.readFile(uploadsPath + "/" + filename, {encoding: 'utf-16le'})
        .then(function (worksheet) {
            var worksheet = workbook.getWorksheet(1);

            console.log(worksheet.getRow(3).getCell(7).value);
        });
}

But then I'm getting this error: 但是然后我得到这个错误:

TypeError [ERR_INVALID_ARG_TYPE]: The "buf" argument must be one of type Buffer, TypedArray, or DataView. TypeError [ERR_INVALID_ARG_TYPE]:“ buf”参数必须是Buffer,TypedArray或DataView类型之一。 Received type object 收到的类型对象

How to deal with it? 怎么处理呢?

First I think ł is a utf-8. 首先,我认为ł是utf-8。

Try printing it in the browser, it may be the console that make it look like this 尝试在浏览器中打印它,可能是控制台使它看起来像这样

Ok, I found a simple solution. 好的,我找到了一个简单的解决方案。

I created function 我创建了功能

function changeEncoding(path) {
    var buffer = fs.readFileSync(path);
    var output = iconv.encode(iconv.decode(buffer, "win1250"), "utf-8");
    fs.writeFileSync(path, output);
}

I simply reading file, and with the help of iconv-lite, firstly decoding from win1250 and then saving the file with utf-8 encoding. 我只是读取文件,并在iconv-lite的帮助下,首先从win1250解码,然后使用utf-8编码保存文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM