Home > database >  Cannot read cyrillic symbols from a .csv file
Cannot read cyrillic symbols from a .csv file

Time:07-30

I need to read some .csv file, get data in .json format and work with it. I'm using npm package enter image description here

If I try to decode file:

const csvToJson = require('convert-csv-to-json');

let json = csvToJson.asciiEncoding().fieldDelimiter(',').getJsonFromCsv("input.csv");
console.log(json);

result is:

enter image description here

When I open a .csv file using AkelPad or notepad - it displays as it has to, and detected format is Win 1251 (ANSI - кириллица). Is there a way to read a file with properly encoding, or to decode a result string?

CodePudding user response:

Try using UTF-8 encoding instead of ASCII.

As a result, change

let json = csvToJson.asciiEncoding().fieldDelimiter(',').getJsonFromCsv("input.csv");

to

let json = csvToJson.utf8Encoding().fieldDelimiter(',').getJsonFromCsv("input.csv");

CodePudding user response:

This is a code to solve the problem:

    const fs = require('fs');
    var iconv = require('iconv-lite');
    const Papa = require('papaparse');

    // read csv file and get buffer
    const buffer = fs.readFileSync("input.csv");
    
    // parse buffer to string with encoding
    let dataString = iconv.decode(buffer, 'win1251');   
    
    // parse string to array of objects
    let config = {
        header: true
    };
    const parsedOutput = Papa.parse(dataString, config);
    console.log('parsedOutput: ', parsedOutput);
  • Related