最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Read CSV headers using Javascript - Stack Overflow

programmeradmin1浏览0评论

I have a csv file with headers that look like this

header1,header2,header3,header4
value1,value2,value3,value4
value1,value2,value3,value4
value1,value2,value3,value4

I just want to read the headers and I've tried.

var keys = [];
        for (var k in unemployment2) keys.push(k);

        alert("total " + keys.length + " keys: " + keys);

but I am getting the row numbers instead.

I have a csv file with headers that look like this

header1,header2,header3,header4
value1,value2,value3,value4
value1,value2,value3,value4
value1,value2,value3,value4

I just want to read the headers and I've tried.

var keys = [];
        for (var k in unemployment2) keys.push(k);

        alert("total " + keys.length + " keys: " + keys);

but I am getting the row numbers instead.

Share Improve this question edited Mar 11, 2014 at 17:17 Ethan Brown 27.3k5 gold badges84 silver badges94 bronze badges asked Mar 11, 2014 at 17:16 B TB T 1612 gold badges6 silver badges15 bronze badges 2
  • 1 I think it needs to be keys.push(unemployment2[k]), that will give you the value of the key-value pair, instead of the key – Ted Commented Mar 11, 2014 at 17:17
  • Reading the MDN documentation about for...in should solve your problem. And then you will hopefully see that you should better use a for loop. – Felix Kling Commented Mar 11, 2014 at 17:21
Add a ment  | 

2 Answers 2

Reset to default 1

So assuming your csv data looking like this:

heading1,heading2,heading3,heading4,heading5,value1_1,value2_1,value3_1,value4_1,value5_1,value1_2,value2_2,value3_2,value4_2,value5_2....

This will read the data and convert an array like this:

[heading1:value1_1 , heading2:value2_1, heading3 : value3_1, heading4 : value4_1, heading5 : value5_1 ],[heading1:value1_2 , heading2:value2_2, heading3 : value3_2, heading4 : value4_2, heading5 : value5_2 ]....

This code will work when your data.txt file is one long string of ma-separated entries, with no newlines:

data.txt:

heading1,heading2,heading3,heading4,heading5,value1_1,...,value5_2

javascript:

$(document).ready(function() {
    $.ajax({
        type: "GET",
        url: "data.txt",
        dataType: "text",
        success: function(data) {processData(data);}
     });
});

function processData(allText) {
    var record_num = 5;  // or however many elements there are in each row
    var allTextLines = allText.split(/\r\n|\n/);
    var entries = allTextLines[0].split(',');
    var lines = [];

    var headings = entries.splice(0,record_num);
        while (entries.length>0) {
        var tarr = [];
        for (var j=0; j<record_num; j++) {
            tarr.push(headings[j]+":"+entries.shift());
        }
        lines.push(tarr);
    }
    // alert(lines);
}

The following code will work on a "true" CSV file with linebreaks between each set of records:

data.txt:

heading1,heading2,heading3,heading4,heading5 value1_1,value2_1,value3_1,value4_1,value5_1 value1_2,value2_2,value3_2,value4_2,value5_2 javascript:

$(document).ready(function() {
    $.ajax({
        type: "GET",
       url: "data.txt",
        dataType: "text",
        success: function(data) {processData(data);}
     });
});

function processData(allText) {
    var allTextLines = allText.split(/\r\n|\n/);
    var headers = allTextLines[0].split(',');
    var lines = [];

    for (var i=1; i<allTextLines.length; i++) {
        var data = allTextLines[i].split(',');
        if (data.length == headers.length) {

            var tarr = [];
            for (var j=0; j<headers.length; j++) {
                tarr.push(headers[j]+":"+data[j]);
            }
            lines.push(tarr);
        }
    }
    // alert(lines);
   }

http://jsfiddle/mblase75/dcqxr/

I used the code below because I wanted to stick to straight JS and my CVS was already neatly formatted so I didn't need additional regular expressions to format them. This was a first step to getting the headers from my csv file, so that I can pare it to another variable in my code.

for (key in unemployment2[0]) {
            console.log(key);
        }
发布评论

评论列表(0)

  1. 暂无评论