逐行读取文件并通过javascript将每行除以空格

时间:2017-05-25 12:12:50

标签: javascript split filereader

我编写了使用HTML5 Filereader API读取文件的代码。逐行读取。但现在我要做的是从每一行获取单独的数据。

如果我读了下面的文件。

2016-8-11 23:13:27  hdhtdht hththth
2016-8-11 23:13:27  edhdhdh  ehdhdhd= dhdhd
2016-8-11 23:13:27  dfgdfgdg eagsdrgergared ergargge
2016-8-11 23:13:27  dgbdfhgb gdhdhgddh
2016-8-11 23:13:27  ggggggggggggg gtrrrrrrrrr

我想将时间,日期和所有其他细节分别归结为一个属性。

{
"_id" : ObjectId("5926c4581d3e69c01f32b074"),
"dat" : "2016-8-11",
"details" : "hdhtdht hththth",
"tim" : "23:13:27"
}

因此,对于每一行,将有三个属性。我试图使用spit()方法进行拆分。但它也将内容分开。 任何人都可以建议吗?

这是我到目前为止写的javascript函数。

$scope.addFileContents= function(readDetails)
{
//read line by line
 var lines = readDetails.split('\n');
 for(var line = 0; line < lines.length; line++){
 //separate by spaces
 var linesSpace = lines[line].split(' ');

  var event = {
    dat: linesSpace[0],
    tim: linesSpace[1],
    details: linesSpace[2],
    space:linesSpace[3]
  };  
  Event.ReadUploadFile(event)
      .success(function () {
          $scope.status = 'Reading the selected file';
          $scope.fileDetails.push(event);
      }).
      error(function (error) {
          $scope.status = 'Unable to insert data: ' + error.message;
      });
}

}

感谢。

3 个答案:

答案 0 :(得分:1)

快速的方法是加入split的结果的最后一个元素(在日期和时间之后)(假设布局总是相同的)

var linesSpace = lines[line].split(' ');

var event = {
    dat: linesSpace[0],
    tim: linesSpace[1],
    details: linesSpace.slice(2).join('')
};  

答案 1 :(得分:1)

使用以下算法:

  1. 将字符串拆分为单独的
  2. 将结果数组中的前两项移至变量
  3. 将剩余的项目重新加入单个字符串
  4. 示例:

    var data    = lines[line].split(' ');
    var date    = data.shift();
    var time    = data.shift();
    var details = data.join(' ');
    

答案 2 :(得分:1)

使用正则表达式来分隔字符串和Array.prototype.map() event,因此它不是对象而是数组。演示1是我相信它将适合您的功能。演示2是实际运行的功能(我不理解你的其余代码。)

详情见演示2

演示1

var readDetails = `2016-8-11 23:13:27  hdhtdht hththth
2016-8-11 23:13:27  edhdhdh  ehdhdhd= dhdhd
2016-8-11 23:13:27  dfgdfgdg eagsdrgergared ergargge
2016-8-11 23:13:27  dgbdfhgb gdhdhgddh
2016-8-11 23:13:27  ggggggggggggg gtrrrrrrrrr`;

$scope.addFileContents = function(readDetails) {
  var rgx = /(\d{4}-\d?-\d{1,2})\s(\d\d:\d\d:\d\d)\s\s(.*)/g;
  
  var cap = `$1|$2|$3`;
  
  var lines = readDetails.split('\n');
  
  var event = lines.map(function(line, idx) {
    var data = (line.replace(rgx, cap)).split('|');
    var frag = {
      date: data[0],
      time: data[1],
      details: data[2]
    };
    return frag;
  });
  
  console.log(event);

  Event.ReadUploadFile(event)
    .success(function() {
      $scope.status = 'Reading the selected file';
      $scope.fileDetails.push(event);
    }).
  error(function(error) {
    $scope.status = 'Unable to insert data: ' + error.message;
  });
}

演示2

var readDetails = `2016-8-11 23:13:27  hdhtdht hththth
2016-8-11 23:13:27  edhdhdh  ehdhdhd= dhdhd
2016-8-11 23:13:27  dfgdfgdg eagsdrgergared ergargge
2016-8-11 23:13:27  dgbdfhgb gdhdhgddh
2016-8-11 23:13:27  ggggggggggggg gtrrrrrrrrr`;

/* Sloppy regex:
{4 digits}-{ 1 or 2 digits}-{1 to 2 digits}{space}
{2 digits}:{2 digits}:{2 digits}{2 spaces}{Anything until end of line}
*/
var rgx = /(\d{4}-\d?-\d{1,2})\s(\d\d:\d\d:\d\d)\s\s(.*)/g;

/* 3 capture groups delimited by a "|"
|| If you need to have any repetitive text included in each line
|| this is a good place to insert it.
*/
var cap = `$1|$2|$3`;

// An array of strings
var lines = readDetails.split('\n');

// map() each string of array to...
var event = lines.map(function(line, idx) {

  /* replace() each match on string so that it's
  || split() 3 ways delimited by the "|"
  */
  var data = (line.replace(rgx, cap)).split('|');

  /* The 3 new strings are assigned to a property
  || of an object literal
  */
  var frag = {
    date: data[0],
    time: data[1],
    details: data[2]
  };
  /* Each object literal is then returned as an
  || element of an array
  */
  return frag;
});
console.log(event);