比较PHP中2个csv文件的内容
有人知道比较两个csv文件的内容并报告相同行的最佳方法吗比较PHP中2个csv文件的内容,php,csv,Php,Csv,有人知道比较两个csv文件的内容并报告相同行的最佳方法吗 “相同”是指每列的值相同的记录。您有文件A和文件B 解析文件A并为每一行创建对象,并将一行的内容存储在一个对象中。创建对象时,将其存储在数组中 对文件B执行相同的操作 现在有了两个数组,第一个数组存储文件A中的所有行数据,另一个数组存储文件B中的行数据 现在,您需要遍历第一个数组,首先遍历数组A中的每个对象,扫描数组B并检查B中是否有相同的对象。如果数组A中的所有元素都通过了此操作。这意味着它们是相同的。否则,中断。您有文件A和文件B 解
“相同”是指每列的值相同的记录。您有文件A和文件B 解析文件A并为每一行创建对象,并将一行的内容存储在一个对象中。创建对象时,将其存储在数组中 对文件B执行相同的操作 现在有了两个数组,第一个数组存储文件A中的所有行数据,另一个数组存储文件B中的行数据
现在,您需要遍历第一个数组,首先遍历数组A中的每个对象,扫描数组B并检查B中是否有相同的对象。如果数组A中的所有元素都通过了此操作。这意味着它们是相同的。否则,中断。您有文件A和文件B 解析文件A并为每一行创建对象,并将一行的内容存储在一个对象中。创建对象时,将其存储在数组中 对文件B执行相同的操作 现在有了两个数组,第一个数组存储文件A中的所有行数据,另一个数组存储文件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";
?>
我认为这就是维德勋爵所说的实际准则:
#!/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";
?>
rlCH的代码示例有一点问题,即 无法处理多行csv 只能处理一个方向上的差异 在第一个差异处停止 虽然对于op来说可能已经足够了,但我正在寻找一种方法来正确比较两个多行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;
}
?>
rlCH的代码示例有一点问题,即 无法处理多行csv 只能处理一个方向上的差异 在第一个差异处停止 虽然对于op来说可能已经足够了,但我正在寻找一种方法来正确比较两个多行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;
}
?>
你不能在命令行使用diff来比较它们?你不能在命令行使用diff来比较它们?我知道这很旧,但是$argv[1]是什么?所以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 Happy hacking!我知道这很旧,但是$argv[1]会是什么呢?因此,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 Happy hacking!