简体   繁体   中英

Uploading And Reading CSV-Parse File

I am trying to read a csv file with multiple same colums using Node CSV-Parser

 'from' ' 'to' 'from' ' 'to' 'from' ' 'to'
 'Germ' ' 'NL' 'Turk' ' 'US' 'Nile' ' 'BR'
 'Germ' ' 'NL' 'Turk' ' 'US' 'Nile' ' 'BR'
 'Germ' ' 'NL' 'Turk' ' 'US' 'Nile' ' 'BR'
 'Germ' ' 'NL' 'Turk' ' 'US' 'Nile' ' 'BR'

And below is my code:

req.file('ratefile').upload(function (err, uploadedFile) {
        if (err) return ResponseService.json(500, err, 'Uploaded Failed');

        console.log(uploadedFile.length) //

        if (uploadedFile.length) {
            rs = fs.createReadStream(uploadedFile[0].fd);
            parser = parse({columns: true, trim: true}, function (err, data){
                var record = {};
                console.log(data);
                _.forEach(data, function(datum, index){
                    var routeName = datum['from'] +"-"+ datum['to'];
                    console.log(routeName);
                    record.name = datum['from'] +"-"+ datum['to'];
                    record.to = datum['to'];
                    record.from = datum['from'];
                });

                return ResponseService.json(200, res, 'file successfully uploaded');
            });
            rs.pipe(parser);
        } else {
            return ResponseService.json(500, res, 'Please upload a file')
        }
    });

And when I run that, it only the last column on the csv file.

  'from' ' 'to'
  'Nile' ' 'BR'
  'Nile' ' 'BR'
  'Nile' ' 'BR'

Any help would be appreciated. Thanks

It will parse into something like

{
  "from": "Germ",
  "to"  : "NL",
  "from": "Turk",
  "to"  : "US",
  ...
}

And of course it's not valid JSON, look at the duplicate keys. So last key value pair will actually overriding previous pair.

In your line

parser = parse({columns: true, trim: true}, function (err, data){

Use function, or null as columns value, like

parse({columns: null, trim: true}

And print out your parsed result, and then modify by your own needs. It can also use function , but I don't pretty sure function can solve your problems, since it will still parsing your single csv line into single object .

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