[英]Delete blank lines in CSV file with PHP or PHPExcel?
我正在嘗試使用PHP以編程方式刪除CSV文件中的空白行。 文件被上傳到站點並使用PHPExcel轉換為CSV。 在數據行之間使用空行生成了一種特殊類型的CSV,我正在嘗試使用PHP清理它們,但沒有任何運氣。 這是此CSV外觀的示例: https : //gist.github.com/vinmassaro/467ea98151e26a79d556
我需要使用PHPExcel或標准PHP函數加載CSV,刪除空白行並保存它。 提前致謝。
編輯:這是當前使用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);
如果要使用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;
}
在Mihai Iorga的注釋中使用str_replace
將起作用。 就像是:
$csv = file_get_contents('path/file.csv');
$no_blanks = str_replace("\r\n\r\n", "\r\n", $csv);
file_put_contents('path/file.csv', $no_blanks);
我復制了您發布的示例中的文本,盡管我必須將“ find”參數更改為"\\r\\n \\r\\n"
而不是"\\r\\n\\r\\n"
,但這仍然有效在每個空白字符尋找線的一個單一的空間。
嘗試這個:
<?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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.