在javascript中解析制表符分隔文件

在javascript中解析制表符分隔文件

本文介绍了在javascript中解析制表符分隔文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法更改服务器端,但我收到的文件如下所示:

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我可以使用Liquid,javascript和jquery。从Liquid,我把内容推到没有风格的div中,希望我能以某种方式解析文件。但是当我在Firebug中查看它时,我只看到我的div与内容为一个巨大的字符串(至少它是它的样子)。

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).

编辑:我需要对数据进行一些数学运算。

I need to do some math on the data.

推荐答案

我不确定这是否正是您正在寻找的,但如果您需要对数据中的数字进行计算,那么您可以尝试这样的事情:

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] - 索引,或行号和第一列

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

行[0] [1] - 第二栏

第[0]行[2] - 第三栏

ex:

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

这篇关于在javascript中解析制表符分隔文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:39