java 删除csv文件中的一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27097810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:12:05  来源:igfitidea点击:

delete a row in csv file

javaopencsv

提问by user3453784

I am appending the data to the last row of a csv. I wanted to delete the existing row and then rewrite it with the appended element. Is there any way of deleting the row in csv? I am using opencsv to read and the write the file. I tried using CSVIterator class. However, it seems the iterator does not support the remove() operation. Here is the code that I tried:

我将数据附加到 csv 的最后一行。我想删除现有的行,然后用附加的元素重写它。有没有办法删除csv中的行?我正在使用 opencsv 来读取和写入文件。我尝试使用 CSVIterator 类。但是,迭代器似乎不支持 remove() 操作。这是我尝试过的代码:

static String[] readLastRecord(File outputCSVFile) throws WAGException {
        checkArgument(outputCSVFile != null, "Output CSV file cannot be null");
        FileReader fileReader = null;
        CSVReader csvFileReader = null;
        CSVIterator csvIterator = null;
        String[] csvLastRecord = null;
        try {
            fileReader = new FileReader(outputCSVFile);
            csvFileReader = new CSVReader(fileReader, ',', '\'',
                    csvRowCount - 1);
            csvIterator = new CSVIterator(csvFileReader);
            while (csvIterator.hasNext()) {
                csvLastRecord = csvIterator.next();
                csvIterator.remove();
            }
        } catch (IOException ioEx) {
            throw new WAGException(
                    WAGInputExceptionMessage.FILE_READ_ERR.getMessage());
        } finally {
            try {
                if (csvFileReader != null)
                    csvFileReader.close();
            } catch (IOException ioEx) {
                throw new WAGException(
                        WAGInputExceptionMessage.FILE_CLOSE_ERR.getMessage());
            }
        }
        return csvLastRecord;
    }

回答by Wladislaw

i just found an answer. Hope it helps.

我刚刚找到了一个答案。希望能帮助到你。

You need to read the csv, add elements to the list string, remove specific row from it with allelements.remove(rowNumber)and then write the list string back to the csv file.

您需要读取 csv,将元素添加到列表字符串中,使用allelements.remove(rowNumber)从中删除特定行,然后将列表字符串写回 csv 文件。

The rowNumber is an intwith row number.

rowNumber 是一个带有行号的int

CSVReader reader2 = new CSVReader(new FileReader(filelocation));
List<String[]> allElements = reader2.readAll();
allElements.remove(rowNumber);
FileWriter sw = new FileWriter(filelocation);
CSVWriter writer = new CSVWriter(sw);
writer.writeAll(allElements);
writer.close();

Look at this example from opencsv opencsv example

从 opencsv opencsv 示例中查看此示例

回答by mehulkumar darji

use unset to remove the row in csv

使用 unset 删除 csv 中的行

function readCSV($csvFile){
   $file_handle = fopen($csvFile, 'r');
   while (!feof($file_handle) ) {
      $line_of_text[] = fgetcsv($file_handle, 1024);
   }
   fclose($file_handle);
   return $line_of_text;
}

$csvFile1 = '../build/js/snowcem.csv';
$csv1 = readCSV($csvFile1); 
//specified row number want to delete on place of $id
unset($csv1[$id]);
$file = fopen("../build/js/snowcem.csv","w");

foreach ($csv1 as $file1) {
    $result = [];
    array_walk_recursive($file1, function($item) use (&$result) {
    $item = '"'.$item.'"';
        $result[] = $item;
    });
    fputcsv($file, $result);
}

fclose($file);