0

我正在尝试使用 PHP 修改上传的 CSV 文件。更具体地说,我希望能够删除/删除列。

我有以下 CSV 文件(+100k 行):

,news_headline,news_article,news_category
0,"50-year-old problem of biology solved by Artificial Intelligence"," DeepMind's AI system .....",technology   

我想要做的是保存 CSV 文件的新副本,但仅包含列:news_headlinenews_category.

这是我目前的设置

use League\Csv\Reader;
use League\Csv\Writer;

//Read in current file.
$path = 'data.csv';
$reader = Reader::createFromPath($path);
$headers = ['news_headline', 'news_category'];
$records = $reader->getRecords($headers);

//Create a new file (override)
$writer = Writer::createFromPath($path);

//Insert new records
$writer->insertAll($records);

上面的代码输出:

,news_headline
0,"50-year-old problem of biology solved by Artificial Intelligence"

我期待的地方:

news_headline,news_category
"50-year-old problem of biology solved by Artificial Intelligence", Technology

4

0 回答 0