[英]How to parse a text file with delimiter using FileReader
I working on an Angular project which I have to upload a .txt
file then parse all its lines loop over them.我在一个 Angular 项目上工作,我必须上传一个
.txt
文件然后解析它的所有行循环遍历它们。 I used this peace of code but it just returns me a text format just like opening it in notepad and that's not what I want, my goal is to every single data with the delimiter ;
我使用了这种代码的和平,但它只是返回一个文本格式,就像在记事本中打开它一样,这不是我想要的,我的目标是每个数据都有分隔符
;
and console that in an array ob objects.并在数组 ob 对象中对其进行控制台。
this is my code:这是我的代码:
fileChangeListener($event: any): void {
const file = $event.target.files[0];
let fileReader = new FileReader();
fileReader.onload = (e) => {
let data = fileReader.result;
console.log("FileREAAAAAAAAAAADER \n" + data);
}
fileReader.readAsText(file);
this is my .txt
file structure:这是我的
.txt
文件结构:
1234;06/07/22;06/07/22;VRT; ;31070;some String content;some String content; ;147.10;A;1234
1234;06/07/22;06/07/22;VRT; ;31070;some String content;some String content; ;147.10;A;1234
1234;06/07/22;06/07/22;VRT; ;31070;some String content;some String content; ;147.10;A;1234
1234;06/07/22;06/07/22;VRT; ;31070;some String content;some String content; ;147.10;A;1234
1234;06/07/22;06/07/22;VRT; ;31070;some String content;some String content; ;147.10;A;1234
in console, the code I wrote displays just like the above structure where the output should be like this:在控制台中,我编写的代码显示就像上面的结构,其中 output 应该是这样的:
I have modified the your code to make a string[][] like you needed.我已经修改了您的代码以制作您需要的 string[][] 。
Not knowing what you want to do with the data, it is just local to that function.不知道您想对数据做什么,它只是 function 的本地数据。
dummyArr is what you want:) dummyArr 是你想要的:)
Kept it kinda bland so you can modify it to your future needs保持它有点平淡,以便您可以根据您未来的需要对其进行修改
Hope this helps!希望这可以帮助!
fileChangeListener(event: any): void {
console.log("submitted here")
const file = event.target.files[0];
let fileReader = new FileReader();
fileReader.onload = (e) => {
let data = fileReader.result;
console.log("FileREAAAAAAAAAAADER \n" + data);
this.parseData(data)
}
fileReader.readAsText(file);
}
parseData(data: string | ArrayBuffer | null){
var dummyArr: string[][] = []
var eachLine = data?.toString().split('\n');
eachLine?.forEach((line: string) => {
let arr = []
let str = ""
for(var i = 0; i < line.length; i++){
if(line[i] == ';'){
arr.push(str)
str = ""
}else{
str += line[i]
}
}
arr.push(str)
dummyArr.push(arr)
})
console.log(dummyArr);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.