Comments (10)
the Countable interface has been added to the Reader class in the upcoming version 9.0
from csv.
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.
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.
@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.
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.
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.
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.
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.
@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.
+1
from csv.
Related Issues (20)
- Any Idea why I am getting this error? Please help HOT 4
- Prevent character encoding HOT 2
- PHPStan errors with versions 9.9 and 9.10 HOT 2
- Add CSV column filtering and re-ordering during parsing
- Calls to rewind or fseek that fail are ignored silently HOT 5
- How to read from STDIN instead of a file HOT 2
- Is Writer::insertAll() faster than Writer::insertOne() or not? HOT 2
- how to read csv without BOM? HOT 4
- getRecords from PART of columns and random sort HOT 2
- getRecords with defined header produce wrong results HOT 1
- Add object mapping HOT 12
- Enable protection against long lines HOT 6
- Preformatting data before conversion HOT 3
- TabularDataReader::getRecords()->current() can return NULL HOT 3
- php version is locked to 8.1, it causes issues with php upgrade to 8.2 or greater HOT 1
- Parsing CSV with escape character before enclosure HOT 2
- Feature request: Trim parsed csv strings HOT 2
- Since 9.12.0 TabularDataReader::getRecords is stricter - exception is thrown with string keys for header HOT 10
- Enclosure not works properly HOT 2
- Is there a simple way to format xls into csv before write/read on it
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from csv.