如何删除CSV文件的最后一行

时间:2015-08-04 07:31:35

标签: php csv

<?php


                $row = 0;
                $handle = fopen("data20150804.log", "r");
                while (!feof($handle) ) {

                    $line_of_text = fgetcsv($handle, 1024, ",");
                    $num = count($line_of_text);
                    $timeStamp = $line_of_text[0];
                    $visibility = $line_of_text[1]; 
                    $pressure = $line_of_text[2];
                    $rain = $line_of_text[3];
                    $temperature = $line_of_text[4];    
                    $humidity = $line_of_text[5];
                    $dewpoint = $line_of_text[6];
                    $R1windSpeed = $line_of_text[7];    
                    $R1wSpeed3s = $line_of_text[8];
                    $R1wSpeed2min = $line_of_text[9];   
                    $R1wSpeed10min = $line_of_text[10];
                    $R1windDirDeg = $line_of_text[11];
                    $R1wDirDeg3s = $line_of_text[12];   
                    $R1wDirDeg2min = $line_of_text[13];
                    $R1wDirDeg10min = $line_of_text[14];
                    $R2windSpeed = $line_of_text[15];   
                    $R2wSpeed3s = $line_of_text[16];
                    $R2wSpeed2min = $line_of_text[17];  
                    $R2wSpeed10min = $line_of_text[18];
                    $R2windDirDeg = $line_of_text[19];
                    $R2wDirDeg3s = $line_of_text[20];   
                    $R2wDirDeg2min = $line_of_text[21];
                    $R2wDirDeg10min = $line_of_text[22];                        
                    $gust = $line_of_text[23];  


                    //$row++;
                }

                fclose($handle);
   ?>

我的csv文件会在几秒钟内保持生成数据。 我想删除csv的最后一行,因为我的系统正在读取最后一行数据而我的最后一行是空白的。

2 个答案:

答案 0 :(得分:1)

如果您检查fgetcsv的documentation,您会看到有关空行返回的说明。所以如果你在以后做检查:

$line_of_text = fgetcsv($handle, 1024, ",");

遵循:

if (count($line_of_text) == 0 || $line_of_text[0] === null) {
    continue;
}

这应该做你需要的。

答案 1 :(得分:0)

试试这个

<?php
$row = 0;
$file = new SplFileObject("data20150804.log.csv");

$file->setFlags(SplFileObject::READ_CSV);
$file->setFlags(SplFileObject::SKIP_EMPTY);
foreach ($file as $line_of_text) {
    $num = count($line_of_text);
                    $timeStamp = $line_of_text[0];
                    $visibility = $line_of_text[1]; 
                    $pressure = $line_of_text[2];
                    $rain = $line_of_text[3];
                    $temperature = $line_of_text[4];    
                    $humidity = $line_of_text[5];
                    $dewpoint = $line_of_text[6];
                    $R1windSpeed = $line_of_text[7];    
                    $R1wSpeed3s = $line_of_text[8];
                    $R1wSpeed2min = $line_of_text[9];   
                    $R1wSpeed10min = $line_of_text[10];
                    $R1windDirDeg = $line_of_text[11];
                    $R1wDirDeg3s = $line_of_text[12];   
                    $R1wDirDeg2min = $line_of_text[13];
                    $R1wDirDeg10min = $line_of_text[14];
                    $R2windSpeed = $line_of_text[15];   
                    $R2wSpeed3s = $line_of_text[16];
                    $R2wSpeed2min = $line_of_text[17];  
                    $R2wSpeed10min = $line_of_text[18];
                    $R2windDirDeg = $line_of_text[19];
                    $R2wDirDeg3s = $line_of_text[20];   
                    $R2wDirDeg2min = $line_of_text[21];
                    $R2wDirDeg10min = $line_of_text[22];                        
                    $gust = $line_of_text[23];
}
?>

我希望它能帮到你。