Is it possible to remove all blank ROWS from a CSV file?
I'm trying to count all rows from a CSV file but would like to exclude lines that doesn't contain a value on a specific column or the whole row.
This is what I'm using to count the rows as of the moment.
$import = file($target_path, FILE_SKIP_EMPTY_LINES);
$num_rows = count($import);
echo $num_rows;
sample:
Jun,Bronse,137 Raven,Princeton,TX,75407,2147088671,Nell@Gmail.Com,1990,CHEVROLET,K1500,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
Nella,Brown,111 Venna St,Princeton,TX,75407,2147177671,lakb@Gmail.Com,1993,CHEVROLET,K1500,,
Jun,Bronse,137 Raven,Princeton,TX,75407,2147088671,Nell@Gmail.Com,1990,CHEVROLET,K1500,,
,,,,,,,,,,,,
Jun,Bronse,137 Raven,Princeton,TX,75407,2147088671,Nell@Gmail.Com,1990,CHEVROLET,K1500,,
For empty rows:
Basically what Barmar already posted, but if you need to process (many) large* files, I'd recommend to give
rtrim
(orltrim
) a try, since usually those are a bit faster for a task like yours:*By large I mean that large that it's really worth it. Don't waste your time doing micro-optimizations.
If you don't want to check a specific column, but just filter out rows where all columns are empty, change it to:
use notepad++ to open .csv file. Then search for
^\*s
goto find and replace and click replace all button. This should solve your problem.