简体   繁体   English

制表符分隔的文本文件到javascript中的数组

[英]Tab delimited text file to array in javascript

I have a text file that has multiple data with a format: 我有一个文本文件,其中包含具有格式的多个数据:

1 2016-01-01 17:23:24 0 0 1 1
2 2016-02-01 07:15:23 0 0 2 1
1 2016-03-01 12:13:24 0 0 1 1
3 2016-04-02 13:34:19 0 0 3 1
.....

Code: 码:

<table>
<tr>
  <td colspan="2" style="font-weight:bold;">
    Upload time sheet for yards with biometrics
  </td>
</tr>
<tr>
  <td colspan="2" style="font-weight:bold;">
    <input type="file" name="fileUpload" id="fileUpload" size="40">
  </td>
</tr>
<tr>
  <td colspan="2" >
    &nbsp;
  </td>
</tr>
<tr>
  <th>Date From:</th><th>Date To:</th>
</tr>
<tr>
  <td><input type="date" id="from_date"></td><td><input type="date" id="to_date"></td>
</tr>
  <td colspan="2" align="right">
    <input type="button" value="Process File" id="btn_process_file" onclick="Upload()">
  </td>
</table>

I need to get certain values on each line of the file to be placed in an array. 我需要在要放置在数组中的文件的每一行上获取某些值。 I only need the first 3 "values" for each line in an array. 我只需要数组中每行的前3个“值”。 Based on my sample above, I just need: 根据上面的示例,我只需要:

1 2016-01-01 17:23:24
2 2016-02-01 07:15:23
1 2016-03-01 12:13:24

To make array like: 使数组像:

var x = [[1,2016-01-01,17:23:24][2,2016-02-01,07:15:23][1,2016-03-01,12:13:24]]

You could also use a input file for files like <input type="file" accept='text/plain' onchange='readText(this)'/> 您也可以将输入文件用于<input type="file" accept='text/plain' onchange='readText(this)'/>

so you can get the content of a chosen file by using a file reader: 因此,您可以使用文件阅读器获取所选文件的内容:

function readText(filePath) {
        var output = ""; 
        if(filePath.files && filePath.files[0]) {           
            reader.onload = function (e) {
                output = e.target.result;
                displayContents(output);
            };
            reader.readAsText(filePath.files[0]);
            var result = output.split("\t");
            console.log(result);
        }
}


var result = output.split("\t");

splitts your string to an array. 将您的字符串拆分为一个数组。 Divided by tabs. 按标签划分。

You can also create a GET-Request on a File on your system. 您还可以在系统上的文件上创建GET-Request。

If you dont use jquery, take this: 如果您不使用jquery,请执行以下操作:

var file = "file://your path"
var toread= new XMLHttpRequest();
toread.open("GET", file, false);
toread.onreadystatechange = function ()
{
    if(toread.readyState === 4)
    {
        if(toread.status === 200 || toread.status == 0)
        {
            var allText = toread.responseText;
        }
    }
}
toread.send(null);
var result = allText.split("\\n");

using jquery would make it a bit easier: 使用jquery会使其更容易一些:

$.get('file://your path', function(allText ) {
   var result = (allText.split("\\n");
}, 'text')

You can use it like this. 您可以像这样使用它。

     $.get('file://your path', function(allText ) {
   var result = allText.split("\t");
  //and access them with index value like this
   alert(result.1);
});
function Upload(){
    var files = document.getElementById('fileUpload').files;

    if(files.length){
        var file = files[0]; // first
        var reader = new FileReader();
        reader.onload = onReadFile;
        reader.onerror = onError;
        reader.readAsText(file);
    }
}

function onReadFile(file){
    var txt = file.target.result;
    var list = txt.split('\n');
    var result = list.map(function(item){
        return item.split(' ');
    });

    console.log(result);
}


function onError (){
    //**//
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM