javascript - Uploading And Reading CSV-Parse File -


i trying read csv file 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 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 uploaded');             });             rs.pipe(parser);         } else {             return responseservice.json(500, res, 'please upload file')         }     }); 

and when run that, last column on csv file.

  'from' ' 'to'   'nile' ' 'br'   'nile' ' 'br'   'nile' ' 'br' 

any appreciated. thanks

it parse like

{   "from": "germ",   "to"  : "nl",   "from": "turk",   "to"  : "us",   ... } 

and of course it's not valid json, @ duplicate keys. last key value pair overriding previous pair.

in line

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

use function, or null columns value, like

parse({columns: null, trim: true} 

and print out parsed result, , modify own needs. can use function, don't pretty sure function can solve problems, since still parsing single csv line single object.


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -