Processing a big php file. Rewrite column values
-
I'm trying to solve the challenge of handling the big
csv
File (12194 line) with goods, reading the file with a library https://github.com/parsecsv/parsecsv-for-phpThere's a model code in the file, I need to go through every line and add an early prefix to the model code, for example:
MODELCODE_PREF987
To this end, I write this code using the library methods:<?php set_time_limit(0); require_once('parsecsv.lib.php');
$fileCSV = 'file_pred.csv'; $csv = new parseCSV(); $csv->delimiter = "\t"; $csv->parse($fileCSV); $count = count($csv->data); for($i = 0; $i<$count; $i++){ $ID = $csv->data[$i]['Код модели'] . "_" . rand(0,1000); $csv->data[$i]['Код модели'] = $ID; $csv->save(); }
But the violin depends on data modifications, if it's just a conclusion, it works fast. And most likely,
php
I'm just starting to figure out, so my code is probably wrong. Please provide an example.
(complete to the compendium) record methodprotected function _wfile($file, $string = '', $mode = 'wb', $lock = 2) {
if ($fp = fopen($file, $mode)) {
flock($fp, $lock);
$re = fwrite($fp, $string);
$re2 = fclose($fp);if ($re != false && $re2 != false) { return true; } } return false; }
-
Try to bear it.
$csv->save()
cyclefor()
://... for($i = 0; $i<$count; $i++){ $ID = $csv->data[$i]['Код модели'] . "_" . rand(0,1000); $csv->data[$i]['Код модели'] = $ID; } $csv->save();
You're making it seem that after every change, there's a file going on, and it's enough to keep the file once, at the end, after all the changes are taken into account. I didn't work with this library, but I watched the library code on the guitar and I think it should help.