gpt4 book ai didi

Javascript - 读取多行文本文件行并报告出数组

转载 作者:行者123 更新时间:2023-12-03 04:10:33 27 4
gpt4 key购买 nike

首先,我要感谢那些已经帮助我到达这里的人。我的代码有效,我可以输出一些从文本文件中提取的数字。现在我面临一个挑战,文本文件有多行需要提取,但所有行都以“Numbers:”开头。 IE。如下:

数字:(23.000,12.000),(12.000,11.000),

数字:(13.000,32.000),(42.000,21.000),

数字:(33.000,52.000),(22.000,31.000),

当我的文本文件只有一行“Numbers:”时,我可以提取数字,你能帮我提取多行吗?附:我不想对我的代码进行太多更改是可能的。谢谢。

<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
}

最佳答案

试试这个代码。我对上面的代码进行了以下更改:-

  • 将代码包装到 for 循环中以执行多行
  • 列表数组位于循环外部,用于存储所有行。

我已经在我的机器上测试了这段代码:-

输入:-数字:(23.000,12.000),(12.000,11.000),数字:(13.000,32.000),(42.000,21.000),

输出:-46,24, 24,22, 26,64, 84,42

希望对您有所帮助。

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
}

关于Javascript - 读取多行文本文件行并报告出数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44351670/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com