Created
December 27, 2018 05:04
-
-
Save parzibyte/0cd258c32e18799635c99fab0d91ba3d to your computer and use it in GitHub Desktop.
Leer CSV con Node.JS created by parzibyte - https://repl.it/@parzibyte/Leer-CSV-con-NodeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Ejemplo de lectura de archivo CSV en Node.JS | |
* | |
* @author parzibyte | |
*/ | |
// "Importar" módulos necesarios | |
const fs = require('fs'); // filesystem | |
const csv = require('csv-parse');// Encargado de parsear | |
const parseador = csv({ | |
delimiter: ',',//Delimitador, por defecto es la coma , | |
cast: true, // Intentar convertir las cadenas a tipos nativos | |
comment: '#' // El carácter con el que comienzan las líneas de los comentarios, en caso de existir | |
}); | |
parseador.on('readable', function () { | |
let fila; | |
while (fila = parseador.read()) { | |
console.log("Tenemos una fila:", fila); | |
} | |
}); | |
parseador.on('error', function (err) { | |
console.error("Error al leer CSV:", err.message); | |
}); | |
fs.createReadStream("productos.csv") // Abrir archivo | |
.pipe(parseador) // Pasarlo al parseador a través de una tubería | |
.on("end", function () {// Y al finalizar, terminar lo necesario | |
console.log("Se ha terminado de leer el archivo"); | |
parseador.end(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"requires": true, | |
"lockfileVersion": 1, | |
"dependencies": { | |
"csv-parse": { | |
"version": "4.2.0", | |
"resolved": "https://registry.npmjs.org/csv-parse/-/csv-parse-4.2.0.tgz", | |
"integrity": "sha512-t8KGN+ZKIYUUqXznAIZ723KI3IBILImjhN8VUfAod/VCtn/IpW3R24PYCxl81g4/z4R9DruTVyvavyiw2VRLgw==" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"main": "index.js", | |
"dependencies": { | |
"csv-parse": "4.2.0" | |
} | |
} |
We can make this file beautiful and searchable if this error is corrected: It looks like row 3 should actually have 4 columns, instead of 1. in line 2.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Nombre,Precio de compra,Precio de venta,Cantidad | |
Mouse Logitech,400.0,500,10 | |
# Aquí otro comentario | |
Xiaomi Mi A1,500.0,600,15 | |
# Yo soy un comentario | |
Auriculares Sony,50.0,500,10 | |
Galletas de chocolate,10,12,500 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment