从 CSV 文件中删除行

发布于 2024-09-29 19:05:31 字数 404 浏览 1 评论 0原文

我有 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);
}

不幸的是,数据库不是一个选择。

I have .csv file with 4 columns. What's the easiest way to remove a line identical with the id of the first column? Here's where I got stuck:

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);
}

Unfortunately, databases were not a choice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光沙漏 2024-10-06 19:05:31
$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');
$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');
当爱已成负担 2024-10-06 19:05:31

我最近在取消订阅新闻通讯时做了类似的事情,这是我的代码:

$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');

I recently did a similar thing in for a newsletter unsubscription, heres my code:

$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;
}

this uses the temp file method of writing out the csv line by line to avoid memory errors. Then once the new file has been created, it deletes the original and renames the temp file.

You can modify this code so that it looks for an ID instead of an email address eg:

$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');
随梦而飞# 2024-10-06 19:05:31
$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);
}
$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);
}
倥絔 2024-10-06 19:05:31

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

$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 命令中只有一个可能会失败,从而泄漏第二个命令的句柄。最好独立检查两个句柄并在出现错误时关闭它们。

I have found a solution, that does not need to copy the file.

$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);
}

Note: only one of those fopen commands could fail, leaking the handle for the second one. It would be good, to check both handles independetly and close them on a error.

岛歌少女 2024-10-06 19:05:31

您可以这样做:

$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 标头吗?

You can do:

$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;
    }
}

basically you run through each line and check if the id does NOT match the id sent in the get parameter, if it does not then it writes the line to the new container / variable.

And then rewrite the $new value to the file, this should work ok:

  • How big is the file
  • Do you have a CSV Header on line 0?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文