Example JavaScript code to parse CSV data

前端 未结 12 1736
梦谈多话
梦谈多话 2020-11-21 07:58

Where could I find some JavaScript code to parse CSV data?

相关标签:
12条回答
  • 2020-11-21 08:41

    I have an implementation as part of a spreadsheet project.

    This code is not yet tested thoroughly, but anyone is welcome to use it.

    As some of the answers noted though, your implementation can be much simpler if you actually have DSV or TSV file, as they disallow the use of the record and field separators in the values. CSV, on the other hand, can actually have commas and newlines inside a field, which breaks most regular expression and split-based approaches.

    var CSV = {
        parse: function(csv, reviver) {
            reviver = reviver || function(r, c, v) { return v; };
            var chars = csv.split(''), c = 0, cc = chars.length, start, end, table = [], row;
            while (c < cc) {
                table.push(row = []);
                while (c < cc && '\r' !== chars[c] && '\n' !== chars[c]) {
                    start = end = c;
                    if ('"' === chars[c]){
                        start = end = ++c;
                        while (c < cc) {
                            if ('"' === chars[c]) {
                                if ('"' !== chars[c+1]) {
                                    break;
                                }
                                else {
                                    chars[++c] = ''; // unescape ""
                                }
                            }
                            end = ++c;
                        }
                        if ('"' === chars[c]) {
                            ++c;
                        }
                        while (c < cc && '\r' !== chars[c] && '\n' !== chars[c] && ',' !== chars[c]) {
                            ++c;
                        }
                    } else {
                        while (c < cc && '\r' !== chars[c] && '\n' !== chars[c] && ',' !== chars[c]) {
                            end = ++c;
                        }
                    }
                    row.push(reviver(table.length-1, row.length, chars.slice(start, end).join('')));
                    if (',' === chars[c]) {
                        ++c;
                    }
                }
                if ('\r' === chars[c]) {
                    ++c;
                }
                if ('\n' === chars[c]) {
                    ++c;
                }
            }
            return table;
        },
    
        stringify: function(table, replacer) {
            replacer = replacer || function(r, c, v) { return v; };
            var csv = '', c, cc, r, rr = table.length, cell;
            for (r = 0; r < rr; ++r) {
                if (r) {
                    csv += '\r\n';
                }
                for (c = 0, cc = table[r].length; c < cc; ++c) {
                    if (c) {
                        csv += ',';
                    }
                    cell = replacer(r, c, table[r][c]);
                    if (/[,\r\n"]/.test(cell)) {
                        cell = '"' + cell.replace(/"/g, '""') + '"';
                    }
                    csv += (cell || 0 === cell) ? cell : '';
                }
            }
            return csv;
        }
    };
    
    0 讨论(0)
  • 2020-11-21 08:42

    Here's another solution. This uses:

    • a coarse global regular expression for splitting the CSV string (which includes surrounding quotes and trailing commas)
    • fine-grained regular expression for cleaning up the surrounding quotes and trailing commas
    • also, has type correction differentiating strings, numbers and boolean values

    For the following input string:

    "This is\, a value",Hello,4,-123,3.1415,'This is also\, possible',true
    

    The code outputs:

    [
      "This is, a value",
      "Hello",
      4,
      -123,
      3.1415,
      "This is also, possible",
      true
    ]
    

    Here's my implementation of parseCSVLine() in a runnable code snippet:

    function parseCSVLine(text) {
      return text.match( /\s*(\".*?\"|'.*?'|[^,]+)\s*(,|$)/g ).map( function (text) {
        let m;
        if (m = text.match(/^\s*\"(.*?)\"\s*,?$/)) return m[1]; // Double Quoted Text
        if (m = text.match(/^\s*'(.*?)'\s*,?$/)) return m[1]; // Single Quoted Text
        if (m = text.match(/^\s*(true|false)\s*,?$/)) return m[1] === "true"; // Boolean
        if (m = text.match(/^\s*((?:\+|\-)?\d+)\s*,?$/)) return parseInt(m[1]); // Integer Number
        if (m = text.match(/^\s*((?:\+|\-)?\d*\.\d*)\s*,?$/)) return parseFloat(m[1]); // Floating Number
        if (m = text.match(/^\s*(.*?)\s*,?$/)) return m[1]; // Unquoted Text
        return text;
      } );
    }
    
    let data = `"This is\, a value",Hello,4,-123,3.1415,'This is also\, possible',true`;
    let obj = parseCSVLine(data);
    console.log( JSON.stringify( obj, undefined, 2 ) );

    0 讨论(0)
  • 2020-11-21 08:43

    You can use the CSVToArray() function mentioned in this blog entry.

    <script type="text/javascript">
        // ref: http://stackoverflow.com/a/1293163/2343
        // This will parse a delimited string into an array of
        // arrays. The default delimiter is the comma, but this
        // can be overriden in the second argument.
        function CSVToArray( strData, strDelimiter ){
            // Check to see if the delimiter is defined. If not,
            // then default to comma.
            strDelimiter = (strDelimiter || ",");
    
            // Create a regular expression to parse the CSV values.
            var objPattern = new RegExp(
                (
                    // Delimiters.
                    "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
    
                    // Quoted fields.
                    "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
    
                    // Standard fields.
                    "([^\"\\" + strDelimiter + "\\r\\n]*))"
                ),
                "gi"
                );
    
    
            // Create an array to hold our data. Give the array
            // a default empty first row.
            var arrData = [[]];
    
            // Create an array to hold our individual pattern
            // matching groups.
            var arrMatches = null;
    
    
            // Keep looping over the regular expression matches
            // until we can no longer find a match.
            while (arrMatches = objPattern.exec( strData )){
    
                // Get the delimiter that was found.
                var strMatchedDelimiter = arrMatches[ 1 ];
    
                // Check to see if the given delimiter has a length
                // (is not the start of string) and if it matches
                // field delimiter. If id does not, then we know
                // that this delimiter is a row delimiter.
                if (
                    strMatchedDelimiter.length &&
                    strMatchedDelimiter !== strDelimiter
                    ){
    
                    // Since we have reached a new row of data,
                    // add an empty row to our data array.
                    arrData.push( [] );
    
                }
    
                var strMatchedValue;
    
                // Now that we have our delimiter out of the way,
                // let's check to see which kind of value we
                // captured (quoted or unquoted).
                if (arrMatches[ 2 ]){
    
                    // We found a quoted value. When we capture
                    // this value, unescape any double quotes.
                    strMatchedValue = arrMatches[ 2 ].replace(
                        new RegExp( "\"\"", "g" ),
                        "\""
                        );
    
                } else {
    
                    // We found a non-quoted value.
                    strMatchedValue = arrMatches[ 3 ];
    
                }
    
    
                // Now that we have our value string, let's add
                // it to the data array.
                arrData[ arrData.length - 1 ].push( strMatchedValue );
            }
    
            // Return the parsed data.
            return( arrData );
        }
    
    </script>
    
    0 讨论(0)
  • 2020-11-21 08:44

    Here's my PEG(.js) grammar that seems to do ok at RFC 4180 (i.e. it handles the examples at http://en.wikipedia.org/wiki/Comma-separated_values):

    start
      = [\n\r]* first:line rest:([\n\r]+ data:line { return data; })* [\n\r]* { rest.unshift(first); return rest; }
    
    line
      = first:field rest:("," text:field { return text; })*
        & { return !!first || rest.length; } // ignore blank lines
        { rest.unshift(first); return rest; }
    
    field
      = '"' text:char* '"' { return text.join(''); }
      / text:[^\n\r,]* { return text.join(''); }
    
    char
      = '"' '"' { return '"'; }
      / [^"]
    

    Try it out at http://jsfiddle.net/knvzk/10 or http://pegjs.majda.cz/online. Download the generated parser at https://gist.github.com/3362830.

    0 讨论(0)
  • 2020-11-21 08:48

    Here's an extremely simple CSV parser that handles quoted fields with commas, new lines, and escaped double quotation marks. There's no splitting or regular expression. It scans the input string 1-2 characters at a time and builds an array.

    Test it at http://jsfiddle.net/vHKYH/.

    function parseCSV(str) {
        var arr = [];
        var quote = false;  // 'true' means we're inside a quoted field
    
        // Iterate over each character, keep track of current row and column (of the returned array)
        for (var row = 0, col = 0, c = 0; c < str.length; c++) {
            var cc = str[c], nc = str[c+1];        // Current character, next character
            arr[row] = arr[row] || [];             // Create a new row if necessary
            arr[row][col] = arr[row][col] || '';   // Create a new column (start with empty string) if necessary
    
            // If the current character is a quotation mark, and we're inside a
            // quoted field, and the next character is also a quotation mark,
            // add a quotation mark to the current column and skip the next character
            if (cc == '"' && quote && nc == '"') { arr[row][col] += cc; ++c; continue; }
    
            // If it's just one quotation mark, begin/end quoted field
            if (cc == '"') { quote = !quote; continue; }
    
            // If it's a comma and we're not in a quoted field, move on to the next column
            if (cc == ',' && !quote) { ++col; continue; }
    
            // If it's a newline (CRLF) and we're not in a quoted field, skip the next character
            // and move on to the next row and move to column 0 of that new row
            if (cc == '\r' && nc == '\n' && !quote) { ++row; col = 0; ++c; continue; }
    
            // If it's a newline (LF or CR) and we're not in a quoted field,
            // move on to the next row and move to column 0 of that new row
            if (cc == '\n' && !quote) { ++row; col = 0; continue; }
            if (cc == '\r' && !quote) { ++row; col = 0; continue; }
    
            // Otherwise, append the current character to the current column
            arr[row][col] += cc;
        }
        return arr;
    }
    
    0 讨论(0)
  • 2020-11-21 08:48

    Regular expressions to the rescue! These few lines of code handle properly quoted fields with embedded commas, quotes, and newlines based on the RFC 4180 standard.

    function parseCsv(data, fieldSep, newLine) {
        fieldSep = fieldSep || ',';
        newLine = newLine || '\n';
        var nSep = '\x1D';
        var qSep = '\x1E';
        var cSep = '\x1F';
        var nSepRe = new RegExp(nSep, 'g');
        var qSepRe = new RegExp(qSep, 'g');
        var cSepRe = new RegExp(cSep, 'g');
        var fieldRe = new RegExp('(?<=(^|[' + fieldSep + '\\n]))"(|[\\s\\S]+?(?<![^"]"))"(?=($|[' + fieldSep + '\\n]))', 'g');
        var grid = [];
        data.replace(/\r/g, '').replace(/\n+$/, '').replace(fieldRe, function(match, p1, p2) {
            return p2.replace(/\n/g, nSep).replace(/""/g, qSep).replace(/,/g, cSep);
        }).split(/\n/).forEach(function(line) {
            var row = line.split(fieldSep).map(function(cell) {
                return cell.replace(nSepRe, newLine).replace(qSepRe, '"').replace(cSepRe, ',');
            });
            grid.push(row);
        });
        return grid;
    }
    
    const csv = 'A1,B1,C1\n"A ""2""","B, 2","C\n2"';
    const separator = ',';      // field separator, default: ','
    const newline = ' <br /> '; // newline representation in case a field contains newlines, default: '\n' 
    var grid = parseCsv(csv, separator, newline);
    // expected: [ [ 'A1', 'B1', 'C1' ], [ 'A "2"', 'B, 2', 'C <br /> 2' ] ]
    

    You don't need a parser-generator such as lex/yacc. The regular expression handles RFC 4180 properly thanks to positive lookbehind, negative lookbehind, and positive lookahead.

    Clone/download code at https://github.com/peterthoeny/parse-csv-js

    0 讨论(0)
提交回复
热议问题