first of all I wanted to thank the ones whom have already helped me get here. My code works and I can output some numbers that are being extracted from a text file. Now I'm facing a challenge where the text file has multiple lines that need to be extracted but all lines start with "Numbers:". ie. below:
Numbers: (23.000,12.000),(12.000,11.000),
Numbers: (13.000,32.000),(42.000,21.000),
Numbers: (33.000,52.000),(22.000,31.000),
I can extract the numbers when my text file has only one line "Numbers:" Can you please help me extract multiple lines? PS I would not want to change my code too much is possible. Thanks.
<script type="text/javascript">
var reader = new FileReader();
function readText(that){
if(that.files && that.files[0]){
var reader = new FileReader();
reader.onload = function (e) {
var output1=e.target.result;
var output2=e.target.result;
var output3=e.target.result;
var output4=e.target.result;
//Reads a text file with a line Starting in "Numbers:" that is formated as
following "Numbers: (23.000,12.000),(11.000,22.000),
//multiple numbers read by 2
output1=(output1.split("\n").filter(/./.test, /Numbers:/).map(line => line.split(/,|\(|\)/).filter(number => number != "")[1]).join("\n")* 2).toFixed(3);
output2=(output2.split("\n").filter(/./.test, /Numbers:/).map(line => line.split(/,|\(|\)/).filter(number => number != "")[2]).join("\n")* 2).toFixed(3);
output3=(output3.split("\n").filter(/./.test, /Numbers:/).map(line => line.split(/,|\(|\)/).filter(number => number != "")[3]).join("\n")* 2).toFixed(3);
output4=(output4.split("\n").filter(/./.test, /Numbers:/).map(line => line.split(/,|\(|\)/).filter(number => number != "")[4]).join("\n")* 2).toFixed(3);
var n1 = parseFloat(output1),
n2 = parseFloat(output2),
n3 = parseFloat(output3),
n4 = parseFloat(output4),
//I dont want to print if NaN or 0, that's the reason for list.push and string below
if (n1) {
list.push(n1);
}
if (n2) {
list.push(n2);
}
if (n3) {
list.push(n3);
}
if (n4) {
list.push(n4);
}
//print my numbers "single line" only. Ideally I want to print multiple lines
document.getElementById('inputTextToSave').innerHTML = list.toString().replace(/[^,]+,[^,]+,/g, '$&\n');
//prints the output as the following
//(23.000,12.000)
//(11.000,22.000)
};//end onload()
reader.readAsText(that.files[0]);
}//end if html5 filelist support
}
Try this code. I have made following changes in above code :-
I have tested this code on my machine :-
Input :- Numbers: (23.000,12.000),(12.000,11.000), Numbers: (13.000,32.000),(42.000,21.000),
Output :- 46,24, 24,22, 26,64, 84,42
I hope it helps.
JAVASCRIPT :-
var reader = new FileReader();
function readText(that){
if(that.files && that.files[0]){
var reader = new FileReader();
reader.onload = function (e) {
// By lines
var lines = e.target.result.split('\n');
var list = [];
for(var Fileline = 0; Fileline < lines.length; Fileline++){
var output1 = lines[Fileline];
var output2 = lines[Fileline];
var output3 = lines[Fileline];
var output4 = lines[Fileline];
output1=(output1.split("\n").filter(/./.test, /Numbers:/).map(line => line.split(/,|\(|\)/).filter(number => number != "")[1]).join("\n")* 2).toFixed(3);
output2=(output2.split("\n").filter(/./.test, /Numbers:/).map(line => line.split(/,|\(|\)/).filter(number => number != "")[2]).join("\n")* 2).toFixed(3);
output3=(output3.split("\n").filter(/./.test, /Numbers:/).map(line => line.split(/,|\(|\)/).filter(number => number != "")[3]).join("\n")* 2).toFixed(3);
output4=(output4.split("\n").filter(/./.test, /Numbers:/).map(line => line.split(/,|\(|\)/).filter(number => number != "")[4]).join("\n")* 2).toFixed(3);
var n1 = parseFloat(output1),
n2 = parseFloat(output2),
n3 = parseFloat(output3),
n4 = parseFloat(output4);
if (n1) {
list.push(n1);
}
if (n2) {
list.push(n2);
}
if (n3) {
list.push(n3);
}
if (n4) {
list.push(n4);
}
}
//print my numbers "single line" only. Ideally I want to print multiple lines
console.log(list);
document.getElementById('inputTextToSave').innerHTML = list.toString().replace(/[^,]+,[^,]+,/g, '$&\n');
//prints the output as the following
//(23.000,12.000)
//(11.000,22.000)
};//end onload()
reader.readAsText(that.files[0]);
}//end if html5 filelist support
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.