回答
你有文件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'在命令行比較它們? –