Giter VIP home page Giter VIP logo

Comments (10)

nyamsprod avatar nyamsprod commented on May 13, 2024 6

the Countable interface has been added to the Reader class in the upcoming version 9.0

from csv.

nyamsprod avatar nyamsprod commented on May 13, 2024 1

Alas no there is no quicker way ... or less memory intensive one. maybe something like

use League\Csv\Reader;
$csv = new Reader('/path/to/your/csv/file.csv');
$count = iterator_count($csv->query());

But I have some doubt... you could make some benchmark if you want ... but yes you can close as works for me 👍

from csv.

nyamsprod avatar nyamsprod commented on May 13, 2024

Hi,

Thanks, for you interest in the library. Adding a count wrapper is not as easy as it seems because we are basically using PHP stream capabilities. A CSV can be very small or extremely large. Attempting to count the actual rows can easily consume more memory than intended because the CSV must be stored completely in memory. That's the main reason the library has no count method.

A simple or better solution would be to add this information in some metadata included in the CSV. Since they're no such specification in CSV it comes down to:

  • the CSV producer to write down this specification
  • the CSV consumer to develop a wrapper using the above specification and the current library.

Just like with any other transport format (ie: JSON with, HAL-JSON or JSON-API for example)

from csv.

dereuromark avatar dereuromark commented on May 13, 2024

@nyamsprod I was more thinking in iterating over the content and raising a $count field.

count = 0;
while (read each row) {
    count++;
}

This way the memory would not be the issue, only speed.
If we store it in a protected attribute we can also re-use it in that instance.

from csv.

nyamsprod avatar nyamsprod commented on May 13, 2024

What your are suggesting is already present in the library. I would suggest using the each method as it returns the number of rows iterate over as a result.

use League\Csv\Reader;
$csv = new Reader('/path/to/your/csv/file.csv');
$count = $csv->each(function() {
    return true;
}); 

You can of course attach all the querying options available in the library before calling the each method.

from csv.

dereuromark avatar dereuromark commented on May 13, 2024

Ah ok, and there is probably no quicker way to read all rows.
Would it be justified to get its own convenience wrapper? Or should we close as works-for-me?

from csv.

NoMan2000 avatar NoMan2000 commented on May 13, 2024

Here's how I did it in a class, this gets both the columns and the rows, and makes sure that the rows are filled. I've found that both CSV and XLS files will mark a row filled if it once had data and it's quick deleted. Passing count in by reference, you can see the difference between the rows that are marked as existing but have no values stored in the first element, (or however you wish to pass this.)

protected function countRows($reader)
{
    $count = 0;
    $countRows = $reader->each(function ($rows) use (&$count) {
        if ($count === 0) {
            $this->columns = count($rows);
        }
        $count++;

        if (!empty($rows[0])) {
            return true;
        }
        if (empty($rows[0])) {
            return false;
        }
    });
    unset($count);
    return $countRows;
 }

from csv.

motin avatar motin commented on May 13, 2024

Here is a sample benchmark: Counting the rows in a 8,8mb csv file with 52401 rows took 3.29s when running on a recent Macbook Pro.

from csv.

nyamsprod avatar nyamsprod commented on May 13, 2024

@dereuromark @motin adding the Countable Interface is a WIP in the next major release you can have more information with PR #178 and the proposed API for v9

from csv.

alexjose avatar alexjose commented on May 13, 2024

+1

from csv.

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.