Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
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. 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.

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:

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:

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I have modified the your code to make a string[][] like you needed.

Not knowing what you want to do with the data, it is just local to that function.

dummyArr is what you want :)

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


about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!