Skip to content

Commit 2b17857

Browse files
author
苏青安
committed
新增文件方法:从多个csv文件中快速提取列
1 parent c811899 commit 2b17857

File tree

1 file changed

+75
-5
lines changed

1 file changed

+75
-5
lines changed

src/FileUtils.php

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public static function fileDelete(string $path): bool
170170
/**
171171
* 获取文件中的唯一行(去重)
172172
*
173-
* 可选择保存到指定的输出文件
173+
* 可选择保存到指定的输出文件,内容较大时建议输出到指定文件
174174
*
175175
* @param string $inputFile 指定检查的文件路径
176176
* @param string $outputFile 输出文件路径,如果为空字符串,则不保存结果
@@ -207,7 +207,7 @@ public function writeUniqueLinesToFile(string $inputFile, string $outputFile = '
207207
$line = trim($line);
208208
if ($line !== '' && !isset($hashSet[$line])) {
209209
$hashSet[$line] = true; // 记录唯一值
210-
if ($outputFile !== null) {
210+
if (!is_null($outputFile)) {
211211
fwrite($outputHandle, $line . PHP_EOL); // 写入输出文件
212212
}
213213
}
@@ -233,7 +233,7 @@ public function writeUniqueLinesToFile(string $inputFile, string $outputFile = '
233233
/**
234234
* 从多个文件中获取交集行
235235
*
236-
* 可选择保存到指定的输出文件
236+
* 可选择保存到指定的输出文件,内容较大时建议输出到指定文件
237237
*
238238
* @param array $filePaths 文件路径数组
239239
* @param string|null $outputFile 输出文件路径,如果为空字符串,则不保存结果
@@ -260,7 +260,7 @@ function getCommonLinesFromFiles(array $filePaths, string $outputFile = ''): boo
260260
} finally {
261261
fclose($handle);
262262
}
263-
if ($commonLines === null) {
263+
if (is_null($commonLines)) {
264264
$commonLines = $fileLines; // 初始化交集
265265
} else {
266266
$commonLines = array_intersect_key($commonLines, $fileLines); // 求交集
@@ -276,4 +276,74 @@ function getCommonLinesFromFiles(array $filePaths, string $outputFile = ''): boo
276276
return $result;
277277
}
278278
}
279-
}
279+
280+
/**
281+
* 从多个csv文件中快速提取列
282+
*
283+
* 适用于提取一个文件夹中所有csv文件的特定列,可选择保存到指定的输出文件,内容较大时建议输出到指定文件
284+
*
285+
* @param string $inputFolder 文件夹路径
286+
* @param int $columnIndex 获取第几列数据,从0开始
287+
* @param string $outputFile 输出文件路径,如果为空字符串,则不保存结果
288+
*
289+
* @return bool|array
290+
* @throws Exception
291+
*/
292+
function extractColumnFromCsvFiles(string $inputFolder, int $columnIndex, string $outputFile = '', bool $skipHeader = true): bool|array
293+
{
294+
$hashSet = [];
295+
try {
296+
// 检查输入文件夹是否存在
297+
if (!is_dir($inputFolder)) {
298+
throw new Exception("输入文件夹不存在: $inputFolder");
299+
}
300+
// 打开输出文件(以追加模式写入)
301+
$outputHandle = fopen($outputFile, 'a');
302+
if (!$outputHandle) {
303+
throw new Exception("打开输出文件失败: $outputFile");
304+
}
305+
// 获取文件夹中的所有 CSV 文件
306+
$csvFiles = glob("$inputFolder/*.csv");
307+
if (empty($csvFiles)) {
308+
throw new Exception("在文件夹:$inputFolder 中未发现 CSV 文件");
309+
}
310+
// 逐个处理每个 CSV 文件
311+
foreach ($csvFiles as $file) {
312+
// 打开 CSV 文件
313+
$handle = fopen($file, 'r');
314+
if (!$handle) {
315+
echo "Failed to open file: $file\n";
316+
continue;
317+
}
318+
$lineNumber = 0;
319+
while (($row = fgetcsv($handle)) !== false) {
320+
$lineNumber++;
321+
// 跳过第一行(标题行)
322+
if ($lineNumber == 1 && $skipHeader) {
323+
continue;
324+
}
325+
// 提取指定列的数据
326+
$columnData = $row[$columnIndex] ?? '';
327+
// 如果数据存在且非空,写入到输出文件
328+
if (!empty($columnData)) {
329+
if (!is_null($outputFile)) {
330+
fwrite($outputHandle, $columnData . PHP_EOL); // 写入输出文件
331+
} else {
332+
$hashSet[] = $columnData;
333+
}
334+
}
335+
}
336+
fclose($handle);
337+
}
338+
fclose($outputHandle);
339+
} catch (Exception $e) {
340+
throw new Exception("Error: " . $e->getMessage());
341+
}
342+
// 返回数据
343+
if (!is_null($outputFile)) {
344+
return true;
345+
} else {
346+
return $hashSet;
347+
}
348+
}
349+
}

0 commit comments

Comments
 (0)