简体   繁体   中英

Convert CSV-stream to UTF-8 in Node.js

In advance I have to say I'm new to node.js and may did not understand it fully yet.

I'm stuck at the moment with a rather obvious problem that I can't solve. I am importing a CSV file from a FTP server via the node module " ftp " then I'm receiving the file as a stream which I then convert to a JSON with the node module " csvtojson ".

So far that works wonderfully. But now I noticed that I stumbled over some Umlaut chars from the German language which keep crashing my solution. I found out that I have to convert the file stream to UTF8, but I have no idea how.

I tried it already with the toString() method and several other node modules but that also didn't work.

My code looks like this:

getCsvFromFtp: function (profile) {
    init(profile);
    return new Promise((resolve, reject) => {
        client.on('ready', function () {
            client.get(file, function (err, stream) {
                if (err) {
                    return reject(err);
                }
                stream.once('close', () => {
                    client.end();
                });
                csv({
                    trim: true,
                    delimiter: delimiter,
                }).fromStream(stream, (err, result) => {
                    if (err) {
                        return reject(err);
                    }
                    return resolve(csvFormatterService.groupAndFormatOrders(result));
                });
            });
        });
        client.connect(ftpCredentials);
    });
}

EDIT: I found out that the problem was not my code, but the encoding of the file. the file was Western (ISO 8859-1) encoded.

You can use utf8 module from npm to encode/decode the string.

One more solution you could use is :

convertString = JSON.parse(JSON.stringify(convertString));

Hope this helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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