parsing tab delimited file in javascript

前端 未结 1 1044
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-23 04:24

I can\'t change the server side, but I\'m getting a file that looks like this:

0   20.59339    138402
1   11.20062    75276
2   32.07597    215573
3   12.2029 82         


        
1条回答
  •  不知归路
    2021-01-23 05:02

    I'm not sure if this is exactly what you're looking for, but if you need to do calculations on the numbers in the data, then you could try something like this:

    var data;
    $.get('data.txt', function(d){
        data = d;
    });
    

    Then split that data first, based on newlines:

    data = data.split(/\r?\n/);
    

    Then you can split based on whitespace, and you'll have a reasonable way to look at the data:

    var lines = [];
    for(var i = 0; i < data.length; i++){
        lines.push(data[i].split(/[ ]+/));
    }
    

    Of course, this is a really simple break down... You probably want to change that last part for a bit more ease of access. But, you could just write a function to read a line and manipulate the data accordingly, where

    line[0][0] - the index, or line number and first column

    line[0][1] - the second column

    line[0][2] - third column

    ex:

    >lines[4]
    >["4", "6.800035", "45701"]
    

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