问题
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 82012
4 6.800035 45701
5 0.6548425 4401
6 0.8643445 5809
7 0.7174848 4822
8 0.813457 5467
9 0.7198655 4838
10 0.8152425 5479
11 1.396878 9388
12 1.93953 13035
13 4.410404 29641
14 1.392266 9357
15 0.7592959 5103
16 1.040368 6992
17 1.603107 10774
I have Liquid, javascript, and jquery at my disposal. From Liquid, I shoved the contents into a div with no style, hoping that I could parse the file somehow. But when I looked at it in Firebug, I just see my div with the contents as one giant string (at least that's what it looks like).
Edit: I need to do some math on the data.
回答1:
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"]
来源:https://stackoverflow.com/questions/27495528/parsing-tab-delimited-file-in-javascript