English 中文(简体)
Javacast CSV 图书馆
原标题:JavaScript CSV Parser Library [closed]
Closed. This question is off-topic. It is not currently accepting answers.

Want to improve this question? Update the question so it s on-topic for Stack Overflow.

Closed 10 years ago.

Is there a decent CSV Parser library for JavaScript? I ve used this and that solution so far. In the first solution a new line is never created as a new sub-array, also the code tells so and the second solution does not work on text files formatted in Windows with <CR><LF> , respectively

申请是否足够

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

to the Windows CSV files? This actually works, but I think this is a little bit quirks. Are there csv parser which are more common than a random bloggers solution?

最佳回答

www.un.org/Depts/DGACM/index_spanish.htm 这里容易的解决办法

csv.split(/
|
|
/g)

它处理:

不幸的是,它打破了限制者之间新路线的价值观。

例如,以下条目......

"this is some","valid CSV data","with a 
newline char"

Will break it because the will be mistakenly interpreted as the end of an entry.

要想找到一个完整的解决办法,你最好要创建一种Non-Deterministic Finite State organs)lexer/parser。 如果你听到Chomsky Hierarchy,CSV可被划为三克mar。 这意味着国家跟踪的果园或象征性加工。

我拥有一个完全的RFC 4180兼容的客户-边图书馆,但我只吸引了外部链接的删除令。 如果你重新感兴趣的话,我的发言中就有一个环节;否则,就好了。

我恳请大家从经验中发出公平的警告,社保会认为表面上很容易。 在研究10个执行单位之后,我只看到了3个支离破碎的 par子,他们从事了合理的工作,满足了这一幽灵,他们当中没有一个完全符合《刑法》。 我只得在社区、抽签和抽签下撰写一份。

问题回答

这样做的方法之一是:

// 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 !==  
 ) {
                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 !==  
 ) {
                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 ===  
 ) {
                next( 
 );
                return array;//empty []
            }
            while (ch) {
                array.push( value() );
                white();
                if (ch ===  
 ) {
                    next( 
 );
                    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;
  };
}();

我的职能是坚固的、公正的下降和使用,我希望它会帮助你。

csvToArray v1.3

一项契约(508英特)但符合要求的功能是,将CSV阵列变成2D阵列,符合RFC4180标准。

http://code.google.com/p/csv-to-array/"rel=“nofollow”>http://code.google.com/p/csv-to-array/

共同使用:j Query

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

共同使用: Java

csvAsArray = csvAsString.csvToArray();

Override field separator

csvAsArray = csvAsString.csvToArray("|");

记录超重的分离器

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

Override Skip Header

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

战胜一切

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




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签