mafintosh / csv-parser

Streaming csv parser inspired by binary-csv that aims to be faster than everyone else
MIT License
1.41k stars 134 forks source link

First key in each result is wrapped in single quotes. #166

Closed freesh closed 4 years ago

freesh commented 4 years ago

Expected Behavior

Importing CSV file and all keys without single quotes.

{
    title: 'This is a title',
    theme: 'Work-Life-Balance ',
    region: 'Berlin',
  }

Actual Behavior

Importing CSV file. But console.log shows me the first key of each record is wrapped in single quotes.

{
    'title': 'this is a title',
    theme: 'Work-Life-Balance ',
    region: 'Berlin',
  }

How Do We Reproduce?

This is my data.csv

title;theme;region
myTitle;Work-Life-Balance;Berlin
myTitle2;Work-Life-Balance;Berlin

This is the code:

const csv = require('csv-parser');
const fs = require('fs');
const results = [];

const indexCsv = () => {
    fs.createReadStream('data.csv')
        .pipe(csv({ 
            separator: ';',
            quote: '"'
            }
        ))
        .on('data', (data) => results.push(data))
        .on('end', async () => {
            console.log(results);
            // ....
        })
}

indexCsv();
mattmiley commented 4 years ago

I'm also having this issue.

My csv looks like this..

CODE,ROOF_SYSTEM_GROUP,ROOF_SYSTEM_NAME,ROOF_SYSTEM
BU,BU,BUR Asphalt,BUR-A
BU,BU,BUR Coal Tar,BUR-C
BU,BU,Inverted Roof Assembly,IRMA
jansphil commented 4 years ago

I also had the issue. The root cause was the BOM at the beginning of the csv-file. I resaved the file in normal UTF-8 encoding without BOM and the file could be parsed successfully.

Also see the section in the Readme of the project.

freesh commented 4 years ago

Thx @jasnell that solved the problem. :)