Coder Social home page Coder Social logo

Comments (5)

alberthaff avatar alberthaff commented on May 26, 2024 2

If you want to use header: true, you must remove the first couple of lines before running it through papaparse - here are some suggestions.

Otherwise, you'll have to use header: false and map the columns yourself. You can do it like this:

    this.papa.parse(csv, {
        skipEmptyLines: true,
        header: true,
        complete: (results )=> {
          for (let i = 2; i < results.data.length; i++) { // Notice that i changed i to 2, so that we skip the line 0 and 1.
            let tradehistory = {
              Date: results.data[i][0],
              Side: results.data[i][1],
              (...)
            };
            this.loading = false;
            this.test.push(tradehistory);
          }
          console.log("Parsed: k", results.data);
        }
    });

from ngx-papaparse.

alberthaff avatar alberthaff commented on May 26, 2024

Hi.
I'm sorry - unfortunately the underlying library does not seem to support this. However, you could simply skip / unset the first couple of rows (depending on how you work with the parsed data).

I actually had a similar issue with some bank statements, since the banks sometimes add a few lines with metadata, before the transactions. I solved it by skipping the first X number of items in the outputted array, when working with the data.

from ngx-papaparse.

gitalvininfo avatar gitalvininfo commented on May 26, 2024

Hi @alberthaff.

Thanks for your response. One last thing. Can you spare a minute to point me out where should I skip or slice the first two rows. This is my code for parsing csv.

 this.papa.parse(csv, {
        skipEmptyLines: true,
        header: true,
        complete: (results )=> {
          results.data.splice(0, 1);
          for (let i = 0; i < results.data.length; i++) {
            let tradehistory = {
              Date: results.data[i].Date,
              Side: results.data[i].Side,
              ConfNumber: results.data[i].ConfNumber,
              Stock: results.data[i].Stock,
              Shares: results.data[i].Shares,
              PhpShare: results.data[i].PhpShare,
              Fees: results.data[i].Fees,
              Total: results.data[i].Total,
            };
            this.loading = false;
            this.test.push(tradehistory);
          }
          console.log("Parsed: k", results.data);
        }
      });

As you can see, I am splicing the index 1 in results array which I think will proceed to the 3rd row. Then I am creating a new array which will contain my desired result. However, this does not solve my issue and I am also aware that setting the header to true will create an objects of row, setting to false will create an array for each row. I am actually confused where did you put the code that skips the row.

This is the result of my code.

Capture

The object keys are still the first row.

Any help would be appreaciated, Thank you so much for helping.

from ngx-papaparse.

gitalvininfo avatar gitalvininfo commented on May 26, 2024

Thanks for the patience. Exactly what I was thinking, I cannot do it when header is true. Thanks again. 👍

from ngx-papaparse.

alberthaff avatar alberthaff commented on May 26, 2024

You're welcome

from ngx-papaparse.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.