JavaScript CSV library for parsers

Is there a suitable CSV Parser library for JavaScript ? I used this and what . In the first solution, a new line is never created as a new auxiliary array, the code also reports this, and the second solution does not work in text files formatted in Windows with <CR><LF> , respectively \r\n

Is it enough to apply

 text = text.replace("\r",""); 

for windows CSV files? It really works, but I think it's a bit of a quirk. Is there a csv parser that is more common than a casual solution for bloggers?

+4
source share
4 answers

Here is a "simple" solution

 csv.split(/\r\n|\r|\n/g) 

It processes:

  • \P
  • \ g
  • \ r \ n
  • \ n \ g

Unfortunately, it splits values ​​containing newlines between delimiters.

For example, the following row entry ...

 "this is some","valid CSV data","with a \r\nnewline char" 

Break it because '\ r \ n' will be mistakenly interpreted as the end of the record.

For a complete solution, it is best to create an ND-FSM lexer / parser (non-deterministic state machine). If you've ever heard of the Chomsky hierarchy , CSV can be analyzed as type III grammar. This means char -by-char or stateful token-token processing.

I have a fully RFC 4180 compatible with the client library, but somehow I got the attention of a successful mod for external linking. Where you are interested, there is a link in my profile; otherwise, good luck.

I will give you a fair warning from experience, CSV looks deceptively light on the surface. Having studied dozens / hundreds of implementations, I saw only 3 javascript parsers that did a reasonable job of fulfilling the specification, and not one of them was fully compatible with RFC. I managed to write one thing, but only with the help of the community and a lot of pain.

+4
source

If you work in Node, there is an excellent CSV analyzer that can handle extremely large amounts of data (> GB files) and supports escape characters.

If you work in the JS browser, you can still extract the processing logic from the code so that it works on a string (instead of Node Stream ).

+2
source

Here is one way to do this:

 // based on json_parse from JavaScript The Good Part by D. Crockford var csv_parse = function () { var at, ch, text, error = function (m) { throw { name: 'SyntaxError', message: m, at: at, text: text }; }, next = function (c) { if (c && c !== ch) { error("Expected '" + c + "' instead of '" + ch + "'"); } ch = text.charAt(at); at += 1; return ch; }, //needed to handle "" which indicates escaped quote peek = function () { return text.charAt(at); }, white = function () { while (ch && ch <= ' ' && ch !== '\n') { next(); } }, // if numeric, then return number number = function () { var number, string = word(); number = +string; if (isNaN(number)) { return string; } else { return number; } }, word = function () { var string = ''; while (ch !== ',' && ch !== '\n') { string += ch; next(); } return string; }, // the matching " is the end of word not , // need to worry about "", which is escaped quote quoted = function () { var string =''; if (ch === '"') { while (next()) { if (ch === '"') { //print('need to know ending quote or escaped quote'); // need to know ending quote or escaped quote ("") if (peek() === '"') { //print('maybe double quote near '+string); next('"'); string += ch; } else { next('"') return string; } } else { string += ch; } } return string; } error("Bad string"); }, value = function () { white(); switch(ch) { case '-': return number(); case '"': return quoted(); default: return ch >= '0' && ch <= '9' ? number() : word(); } return number(); }, line = function () { var array = []; white(); if (ch === '\n') { next('\n'); return array;//empty [] } while (ch) { array.push( value() ); white(); if (ch === '\n') { next('\n'); return array;//got something } next(',');// not very liberal with delimiter white(); } }; return function (_line) { var result; text = _line; at = 0; ch = ' '; result = line(); white(); if (ch) { error("Syntax error"); } return result; }; }(); 
+2
source

My function is robust, just go in and use it, I hope it helps you.

csvToArray v1.3

Compact (508 bytes), but compatible function for converting a CSV string to a 2D array that complies with the RFC4180 standard.

http://code.google.com/p/csv-to-array/

General Use: jQuery

  $.ajax({ url: "test.csv", dataType: 'text', cache: false }).done(function(csvAsString){ csvAsArray=csvAsString.csvToArray(); }); 

General Use: Javascript

 csvAsArray = csvAsString.csvToArray(); 

Undo the field separator

 csvAsArray = csvAsString.csvToArray("|"); 

Override Record Separator

 csvAsArray = csvAsString.csvToArray("", "#"); 

Override skip header

 csvAsArray = csvAsString.csvToArray("", "", 1); 

Cancel all

 csvAsArray = csvAsString.csvToArray("|", "#", 1); 
0
source

Source: https://habr.com/ru/post/1381395/


All Articles