繁体   English   中英

逐行读取文件,并通过javascript用空格分隔每一行

[英]Read file line by line and separate each line by spaces by javascript

我编写了使用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; }); } } 

谢谢。

一种快速的方法是仅将split结果的最后一个元素(日期和时间之后) split (这是假设布局始终相同)

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

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

使用以下算法:

  1. 将字符串拆分为单独的单词
  2. 将结果数组中的前2个项目移到变量中
  3. 将其余项目重新连接成一个字符串

例:

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

使用正则表达式将字符串和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); 

暂无
暂无

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

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