2013-10-21 117 views
0

我正在嘗試以編程方式刪除使用PHP的CSV文件中的空白行。文件被上傳到網站並使用PHPExcel轉換爲CSV。一個特定類型的CSV是在數據行之間用空行生成的,我試圖用PHP清理它們,沒有任何運氣。這裏是一個這樣的CSV的例子:https://gist.github.com/vinmassaro/467ea98151e26a79d556用PHP或PHPExcel刪除CSV文件中的空行?

我需要加載CSV,刪除空行,並保存它,使用PHPExcel或標準的PHP函數。提前致謝。

編輯: 以下是目前如何使用PHPExcel進行轉換的代碼片段。這是一個Drupal鉤子的一部分,作用於剛剛上傳的文件。我無法得到PHPExcel removeRow方法的工作原理,因爲它似乎不適用於空行,只有空行數據。

// Load the PHPExcel IOFactory. 
require_once(drupal_realpath(drupal_get_path('module', 'custom')) . '/PHPExcel/Classes/PHPExcel/IOFactory.php'); 

// Load the uploaded file into PHPExcel for normalization. 
$loaded_file = PHPExcel_IOFactory::load(drupal_realpath($original_file->uri)); 
$writer = PHPExcel_IOFactory::createWriter($loaded_file, 'CSV'); 
$writer->setDelimiter(","); 
$writer->setEnclosure(""); 

// Get path to files directory and build a new filename and filepath. 
$files_directory = drupal_realpath(variable_get('file_public_path', conf_path() . '/files')); 
$new_filename = pathinfo($original_file->filename, PATHINFO_FILENAME) . '.csv'; 
$temp_filepath = $files_directory . '/' . $new_filename; 

// Save the file with PHPExcel to the temp location. It will be deleted later. 
$writer->save($temp_filepath); 
+0

考慮到顯示的代碼相關部分在你使用* ...使用PHPExcel轉換爲CSV。* – peterm

+1

'echo str_replace(「\ r \ n \ r \ n」,「\ r \ n」,$ csv);'不起作用? :) –

+0

你使用FTP嗎?如果你在文本模式下傳輸,你可以得到兩倍行結束http://stackoverflow.com/questions/11938942/ftp-binary-x-ascii-automatic-selection –

回答

1

使用str_replace函數在米哈伊Iorga的評論會工作。喜歡的東西:

$csv = file_get_contents('path/file.csv'); 
$no_blanks = str_replace("\r\n\r\n", "\r\n", $csv); 
$file = fopen('path/file.csv', 'w'); 
fwrite($file, $no_blanks); 
fclose($file); 

我複製從您發佈的示例中的文本,這工作,但我不得不改變"\r\n \r\n"而不是"\r\n\r\n",因爲在每個空白的前瞻性線的單一空間。

+1

我不得不稍微調整一下正則表達式,但這種方法效果很好。謝謝! – Vincent

0

試試這個:

<?php 

$handle = fopen("test.csv", 'r'); //your csv file 
$clean = fopen("clean.csv", 'a+'); //new file with no empty rows 

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
    $num = count($data); 
    if($num > 1) 
    fputcsv($clean, $data ,";"); 
} 
fclose($handle); 
fclose($clean); 

?> 

測試在我的本地。

輸出數據:

初始文件:

col1,col2,col3,col4,col5,col6,col7,col8 

0,229.500,7.4,3165.5,62,20.3922,15.1594,0 

1,229.600,8.99608,3156.75,62,15.6863,16.882,0 

2,229.700,7.2549,3130.25,62,16.8627,15.9633,0 

3,229.800,7.1098,3181,62,17.2549,14.1258,0 

清潔CSV文件:

col1 col2 col3 col4 col5 col6 col7 col8 
0  229.500 7.4  3165.5 62 203.922 151.594 0 
1  229.600 899.608 3156.75 62 156.863 16.882 0 
2  229.700 72.549 3130.25 62 168.627 159.633 0 
3  229.800 71.098 3181  62 172.549 141.258 0 
+0

謝謝,我稍後會給這個鏡頭並回報。 – Vincent

2

如果你想使用phpexcel,搜索CSV.php 和編輯這個文件:在文件編輯結束

// Write rows to file 
for ($row = 1; $row <= $maxRow; ++$row) { 
    // Convert the row to an array... 
    $cellsArray = $sheet->rangeToArray('A'.$row.':'.$maxCol.$row, '', $this->preCalculateFormulas); 
    // edit by ger 
    // if last row, then no linebreak will added 
    if($row == $maxRow){ $ifMaxRow = TRUE; }else{ $ifMaxRow = False; } 
    // ... and write to the file 
    $this->writeLine($fileHandle, $cellsArray[0], $ifMaxRow); 
} 

/** 
* Write line to CSV file 
* 
* edit by ger 
* 
* @param mixed $pFileHandle PHP filehandle 
* @param array $pValues  Array containing values in a row 
* @throws PHPExcel_Writer_Exception 
*/ 
private function writeLine($pFileHandle = null, $pValues = null, $ifMaxRow = false) 
{ 
... 
      // Add enclosed string 
      $line .= $this->enclosure . $element . $this->enclosure; 
     } 

insert this following  

     if($ifMaxRow == false){ 
      // Add line ending 
      $line .= $this->lineEnding; 
     }