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-php

    There'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_PREF987To 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-&gt;delimiter = "\t";
    $csv-&gt;parse($fileCSV);
    
    $count = count($csv-&gt;data);
    
    for($i = 0; $i&lt;$count; $i++){
            $ID = $csv-&gt;data[$i]['Код модели'] . "_" . rand(0,1000);    
            $csv-&gt;data[$i]['Код модели'] = $ID;
            $csv-&gt;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 method

     protected 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 &amp;&amp; $re2 != false) {
                return true;
            }
        }
    
        return false;
    }
    



  • Try to bear it. $csv->save() cycle for():

    //...
    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.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2