dongzou1964 2014-09-23 17:34
浏览 36
已采纳

替换PHP中的CSV行,内存错误

I have a csv file that is 2Mb size, and has pipe delimiter. I would like to take the first row and replace its data then resave the file. Here is what I did :

//Creating a new first row with the modified data.
$file = fopen($path,"r");//$path is where the file is located : outputs/my_file.csv
$size = filesize($path);
$firstLine = fgetcsv(fopen($path,"r")); //$firstLine has all the data of the first row as array
fclose($file);
$firstLine = explode("|", $firstLine[0]);//To get each column row
$newHeader = array();
    for($i = 0; $i<sizeof($firstLine ); $i++){
        if($i == 4){
            array_push($newHeader, "modified column in row 1 ");//Only column 4 in row 1 is modified
        }else{
            array_push($newHeader, $firstLine [$i]);
        }
    }
$Header = implode("|", $newHeader);

//Creating the new csv file
$row = 0;
    while (($data = fgetcsv(fopen($path,"r"), "|")) !== false) {
        if($row == 0){
            $data[0] = $Header;
        }
        $newCsvData[] = $data;
    }
    return $newCsvData; //I wanted to display the new content of the csv before saving it

This code should print the new content of the csv file that I will store but I get an error : Allowed memory size of 536870912 bytes exhausted (tried to allocate 332 bytes) How can I do that in a very fast way ? the file is about 19122 row.

Thanks

  • 写回答

2条回答 默认 最新

  • doufu2396 2014-09-23 18:34
    关注

    If it's only 2mb, maybe read the entire file into memory and then write out a new file (overwriting the previous file). Here are some helper functions to help you read and write the file, and I'm certain you're proficient in editing the resulting array:

    /**
      * Reads a file into an array
      *
      * @param $FILE string the file to open
      *
      * @return $lines The Lines of the file as an array
      */
    public static function readFile($FILE) {
    
        $lines = array(); // the array to store each line of the file in
    
        $handle = fopen($FILE, "r"); 
        if ($handle) { 
    
            // $FILE successfully opened for reading, 
    
            while (($line = fgets($handle)) !== false) {
                $lines[] = $line; //add each line of the file to $lines
            } 
    
        } else {
            throw new Exception("error opening the file...");
        } 
    
        fclose($handle); // close the file
    
        return $lines; // return the lines of the file as an array
    }
    
     /**
       * Writes the $lines of a file into $FILE
       *
       * @param $FILE string The file to write
       * @param $lines array An array containing the lines of the file
       *
       * @return $result int|NULL The number of bytes written, or null on failure. See: php.net/fwrite#refsect1-function.fwrite-returnvalues
       */
    public static writeFile($FILE, $lines) {
    
        // Add newline at the end of each line of the array
        // output is now a single string which we will write in one pass 
        // (instead of line-by-line)
        $output = implode("
    ", $lines);
    
        $handle = fopen($FILE, "w+"); 
        if ($handle) { 
    
            // $FILE successfully opened for writing, write to the file
            $result = fwrite($handle, $output);
    
        } else {
            throw new Exception("error opening the file...");
        } 
    
        fclose($handle); // close the file
    
        return $result; // The number of bytes written to the file, or NULL on failure
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助