如何使用 javascript 从 *.CSV 文件中读取数据?

2022-01-30 00:00:00 jquery javascript

我的 csv 数据如下所示:

My csv data looks like this:

标题1,标题2,标题3,标题4,标题5,value1_1,value2_1,value3_1,value4_1,value5_1,value1_2,value2_2,value3_2,value4_2,value5_2....

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 转换成这样的数组?:

How do you read this data and convert to an array like this using Javascript?:

[标题1:值1_1,标题2:值2_1,标题3:值3_1,标题4:值4_1,标题5:值5_1],[标题1:值1_2,标题2:值2_2,标题3:值3_2,标题4:值4_2,标题5: value5_2 ]....

[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 ]....

我已经尝试过这段代码,但没有运气!:

I've tried this code but no luck!:

<script type="text/javascript">
    var allText =[];
    var allTextLines = [];
    var Lines = [];

    var txtFile = new XMLHttpRequest();
    txtFile.open("GET", "file://d:/data.txt", true);
    txtFile.onreadystatechange = function()
    {
        allText = txtFile.responseText;
        allTextLines = allText.split(/
|
/);
    };

    document.write(allTextLines);<br>
    document.write(allText);<br>
    document.write(txtFile);<br>
</script>

推荐答案

注意: 在提醒我有效 CSV 文件中可能出现的所有特殊情况"之前,我制定了这个解决方案,就像转义引号一样.我将为那些想要快速而肮脏的东西的人留下我的答案,但我建议 Evan's answer 准确.

NOTE: I concocted this solution before I was reminded about all the "special cases" that can occur in a valid CSV file, like escaped quotes. I'm leaving my answer for those who want something quick and dirty, but I recommend Evan's answer for accuracy.

当您的 data.txt 文件是一长串以逗号分隔的条目且没有换行符时,此代码将起作用:

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

数据.txt:

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

javascript:

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(/
|
/);
    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);
}

以下代码适用于每组记录之间带有换行符的真实"CSV 文件:

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

数据.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:

javascript:

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

function processData(allText) {
    var allTextLines = allText.split(/
|
/);
    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.net/mblase75/dcqxr/

相关文章