回答
你有文件A和文件B
解析的文件A并为每个行对象和一行的内容存储在一个对象。在创建对象时,将它们存储在一个数组中。
做同样的事情的文件B.
所以,现在你有两个数组,第一个数组存储在文件中的行的所有数据,以及另一阵列B.
现在,您需要迭代通过你的第一个数组,首先在数组A中的每个对象,扫描数组B,并检查B中是否有相同的对象,如果数组A中的所有元素都通过了这个。这意味着他们是理想的。否则,休息。
,我认为这是实际的代码的主维达说:
#!/usr/bin/php
<?
$strFile1 = $argv[1];
$strFile2 = $argv[2];
function parseData($strFilename) {
$strAllData = file($strFilename);
foreach($strAllData as $intLineNum => $strLineData) {
$arrLineData = explode(',',$strLineData);
}
return $arrLineData;
}
$arrFile1 = parseData($strFile1);
$arrFile2 = parseData($strFile2);
$intRow = 0;
foreach($arrFile1 as $intKey => $strVal) {
if(!isset($arrFile2[$intKey]) || ($arrFile2[$intKey] != $strVal)) {
exit("Column $intKey, row $intRow of $strFile1 doesn't match\n");
}
$intRow++;
}
print "All rows match fine.\n";
?>
我知道这是旧的,但'$ argv [1]'是什么? – BandonRandon
所以你做什么Bandon将所有的代码都保存到你的* nix盒子的一个文件(比如“compare-csvs.php”),使它可执行('chmod + x compare-csvs.php') 。然后调用“compare-csv.php”,比如:'compare-csvs.php /path/to/first-csv.csv/path/to/second-csv.csv'快乐黑客! – rICh
有一点问题,与rlCH代码示例,即
- 不能处理多行以csv
- 只能在一个方向上在第一差分
虽然它可能足够的操作我正在寻找一种方法来正确比较两个多行csv文件。 (多行包含跨越多行的数据)所以我花了时间创建一个,我想为什么不分享它。也许它为某人节省了一些时间。
现在,我没有使用命令行PHP,所以如果你想要做的,我建议你改变输入处理和输出(这个输出HTML,所以你可以在浏览器中使用它)
用法; 把脚本和文件在目录 调用脚本比较两个参数,f1和f2 如compareCSV.php?F1 = file1.csv & F2 = file2.csv
<?php
//---- init
$strFileName1=isset($_REQUEST['f1'])?$_REQUEST['f1']:'';
$strFileName2=isset($_REQUEST['f2'])?$_REQUEST['f2']:'';
if (!$strFileName1) { die("I need the first file (f1)"); }
if (!$strFileName2) { die("I need the second file (f2)"); }
try {
$arrFile1 = parseData($strFileName1);
$arrFile2 = parseData($strFileName2);
} catch (Exception $e) {
die($e->getMessage());
}
$rowCount1=count($arrFile1);
$rowCount2=count($arrFile2);
$colCount1=count($arrFile1[0]);
$colCount2=count($arrFile2[0]);
$highestRowCount = $rowCount1>$rowCount2 ? $rowCount1:$rowCount2;
$highestColCount = $colCount1>$colCount2 ? $colCount1:$colCount2;
$row = 0;
$err = 0;
//---- code
echo "<h2>comparing $strFileName1 and $strFileName2</h2>";
echo "\n<table border=1>";
echo "\n<tr><th>Err<th>Row#<th>Col#<th>Data in $strFileName1<th>Data in $strFileName2";
while($row<$highestRowCount) {
if(!isset($arrFile1[$row])) {
echo "\n<tr><td>Row missing in $strFileName1<th>$row";
$err++;
} elseif(!isset($arrFile1[$row])) {
echo "\n<tr><td>Row missing in $strFileName2<th>$row";
$err++;
} else {
$col=0;
while($col<$highestColCount) {
if (!isset($arrFile1[$row][$col])) {
echo "\n<tr><td>Data missing in $strFileName1<td>$row<td>$col<td><td>".htmlentities($arrFile2[$row][$col]);
$err++;
} elseif (!isset($arrFile2[$row][$col])) {
echo "\n<tr><td>Data missing in $strFileName1<td>$row<td>$col<td>".htmlentities($arrFile1[$row][$col]) ."<td>";
$err++;
} elseif ($arrFile1[$row][$col] != $arrFile2[$row][$col]) {
echo "\n<tr><td>Data mismatch";
echo "<td>$row <td>$col";
echo "<td>".htmlentities($arrFile1[$row][$col]);
echo "<td>".htmlentities($arrFile2[$row][$col]);
$err++;
}
$col++;
}
}
$row++;
}
echo "</table>";
if (!$err) {
echo "<br/>\n<br/>\nThe two csv data files seem identical<br/>\n";
} else {
echo "<br/>\n<br/>\nThere are $err differences";
}
//---- functions
function parseData($strFilename) {
$arrParsed = array();
$handle = fopen($strFilename , "r");
if ($handle) {
while (!feof($handle)) {
$data = fgetcsv($handle , 0 , ',' , '"');
if (empty($data)) continue; //empty row
$arrParsed[]=$data;
}
fclose($handle);
} else {
throw new Exception("File read error at $strFilename");
}
return $arrParsed;
}
?>
- 1. 比较2个excel文件的内容
- 2. 比较Unix中两个csv的内容
- 3. 比较2个csv文件的列
- 4. 比较两个csv文件中的内容
- 5. 比较c#中2个文件的内容#
- 6. 列车和测试仪不兼容:比较2个csv文件
- 7. 比较PHP中两个CSV文件内存不足
- 8. 在C中比较两个csv文件#
- 9. 比较2个数组的内容
- 10. 比较两个CSV文件,
- 11. 比较文件内容
- 12. 比较文本和csv文件的内容
- 13. 在2个CSV文件中计数行进行比较
- 14. 比较内容中有多个文件的两个文件夹
- 15. 比较两个csv文件中的列
- 16. 比较Python中的文件内容
- 17. 比较csv文件
- 18. 使用awk比较shell中的2个csv文件
- 19. 在红宝石中csv/txt文件内容比较。 - 用于多对文件
- 20. 在Python中比较2个文件
- 21. 比较2个文件夹
- 22. 比较2个XML文件
- 23. 比较2个txt文件
- 24. 比较两个csv文件的内容并将其存储到变量中
- 25. 比较C#2项文本文件的内容,并删除同一内容
- 26. 比较2个目录中的文件
- 27. 比较vbscript中的2个文件
- 28. 比较2行的文件的内容,并用perl
- 29. 比较两个CSV文件的Python
- 30. delphi比较文本文件内容
你不能只需使用'diff'在命令行比较它们? –