简体   繁体   English

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

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

I wrote the code to read the file using HTML5 Filereader API .reading line by line is done.but now what i want to do is get separate data from each line. 我编写了使用HTML5 Filereader API读取文件的代码。完成了一行一行的读取。但是现在我要做的是从每一行获取单独的数据。

If i read a file like below. 如果我阅读如下文件。

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

i want to get separately time , date and all other details to one attribute. 我想分别获取时间,日期和所有其他详细信息到一个属性。

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

so for each line there will be three attributes.i attempted to split using spit() method.but it separate the contents as well. 因此对于每一行都有三个属性。我尝试使用spit()方法进行拆分。但它也将内容分开。 can anyone suggest a way?? 有人可以建议一种方法吗?

this is javascript fuction i wrote so far. 这是我到目前为止编写的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; }); } } 

Thanks. 谢谢。

A quick way would be to just join back last elements (after date and time) of the result from split (this is assuming the layout is always the same) 一种快速的方法是仅将split结果的最后一个元素(日期和时间之后) split (这是假设布局始终相同)

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

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

Use the following algorithm: 使用以下算法:

  1. Split the string into separate words 将字符串拆分为单独的单词
  2. Shift the first 2 items off the resultant array into variables 将结果数组中的前2个项目移到变量中
  3. Join the remaining items back into a single string 将其余项目重新连接成一个字符串

Example: 例:

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

Used regex to separate the strings and Array.prototype.map() for event so it's not an object but an array. 使用正则表达式将字符串和Array.prototype.map()分隔为event因此它不是对象,而是数组。 Demo 1 is how I believe it'll fit into your function. 演示1是我相信它将适合您的功能。 Demo 2 is the function actually functioning (I don't understand the rest of your code.) 演示2是实际起作用的功能(我不理解您的其余代码。)

Details are commented in Demo 2 演示2中对详细信息进行了评论

Demo 1 演示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; }); } 

Demo 2 演示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