从CSV文件中删除行

8

我有一个包含4列的csv文件。最简单的方法是如何删除与第一列id相同的行? 这就是我卡住的地方:

if($_GET['id']) {
    $id = $_GET['id'];
    $file_handle = fopen("testimonials.csv", "rw");

    while (!feof($file_handle) ) {
        $line_of_text = fgetcsv($file_handle, 1024);    
        if ($id == $line_of_text[0]) {
            // remove row
        }
    }
    fclose($file_handle);
}

很遗憾,数据库不是一个选择。

5个回答

8
$table = fopen('table.csv','r');
$temp_table = fopen('table_temp.csv','w');

$id = 'something' // the name of the column you're looking for

while (($data = fgetcsv($table, 1000)) !== FALSE){
    if(reset($data) == $id){ // this is if you need the first column in a row
        continue;
    }
    fputcsv($temp_table,$data);
}
fclose($table);
fclose($temp_table);
rename('table_temp.csv','table.csv');

3

我最近为取消订阅邮件做了类似的事情,这是我的代码:

$signupsFile = 'newsletters/signups.csv';
$signupsTempFile = 'newsletters/signups_temp.csv';
$GLOBALS["signupsFile"] = $signupsFile;
$GLOBALS["signupsTempFile"] = $signupsTempFile;


function removeEmail($email){
    $removed = false;
    $fptemp = fopen($GLOBALS["signupsTempFile"], "a+");
    if (($handle = fopen($GLOBALS["signupsFile"], "r")) !== FALSE) {
        while (($data = fgetcsv($handle)) !== FALSE) {
        if ($email != $data[0] ){
            $list = array($data);
            fputcsv($fptemp, $list);
            $removed = true;
        }
    }
    fclose($handle);
    fclose($fptemp);
    unlink($GLOBALS["signupsFile"]);
    rename($GLOBALS["signupsTempFile"], $GLOBALS["signupsFile"]);
    return $removed;
}

这里使用临时文件的方法按行写出CSV,以避免内存错误。然后在创建新文件后,它会删除原始文件并重命名临时文件。

您可以修改此代码,使其查找ID而不是电子邮件地址,例如:

$id = $_GET['id'];  
$fptemp = fopen('testimonials-temp.csv', "a+");
if (($handle = fopen('testimonials.csv', "r")) !== FALSE) {
    while (($id= fgetcsv($handle)) !== FALSE) {
    if ($id != $data[0] ){
        $list = array($data);
        fputcsv($fptemp, $list);
    }
}
fclose($handle);
fclose($fptemp);
unlink('testimonials.csv');
rename('testimonials-temp.csv','testimonials.csv');

1
$id = $_GET['id'];
if($id) {
    $file_handle = fopen("testimonials.csv", "w+");
    $myCsv = array();
    while (!feof($file_handle) ) {
        $line_of_text = fgetcsv($file_handle, 1024);    
        if ($id != $line_of_text[0]) {
            fputcsv($file_handle, $line_of_text);
        }
    }
    fclose($file_handle);
}

4
你忘记了将csv数组写入另一个文件的部分。你也不想将整个csv存储在一个数组中,因为这可能会导致内存耗尽错误。最好是逐步写出csv。 - Byron Whitlock

0

你可以做:

$new = '';
while (!feof($file_handle))
{
    $line_of_text = fgetcsv($file_handle, 1024);    
    if ($id != $line_of_text[0])
    {
         $new .= implode(',',$line_of_text) . PHP_EOL;
    }
}

基本上,您需要运行每一行并检查ID是否与GET参数中发送的ID不匹配,如果不匹配,则将该行写入新容器/变量。

然后将$new值重写到文件中,这应该可以正常工作:

  • 文件有多大?
  • 0行上是否有CSV标题?

0

我找到了一个解决方案,不需要复制文件。

$file = 'testimonials.csv'
// open two handles on the same file
$input = fopen($file ,'r'); // read mode
$output = fopen($file, 'c'); // write mode

if($input !== FALSE && $output !== FALSE) { // check for error
    while (($data = fgetcsv($input, $CSVLIMIT, $sep)) !== FALSE) {
        if(reset(data) == $id) {
            continue;
        }
        fputcsv($output, $data, $sep);
    }

    // close read handle
    fclose($input);
    // shorten file to remove overhead created by this procedure
    ftruncate($output, ftell($output));
    fclose($output);
}

注意:这些fopen命令中只有一个可能失败,泄漏第二个的句柄。最好独立检查两个句柄并在错误时关闭它们。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接