يتيح لك CSVComparetool العثور على اختلافات بين ملفين CSV استنادًا إلى معرف عمود محدد. يوفر طريقة بسيطة لمقارنة محتويات ملفين CSV وتحديد السجلات الموجودة في ملف واحد ولكن ليس في الآخر.
ملحوظة
عند مقارنة ملفات CSV للاختلافات ، تأكد من تزويد CSV بمزيد من الإدخالات في المرتبة الثانية. على سبيل المثال ، إذا كان CSV-1 يحتوي على قائمة تضم 34 اسمًا ، ويجب أن يتم تعيين CSV-2 قائمة بـ 40 ، فيجب تعيين CSV-2 كمسار CSV الثاني الذي تم تمريره من أجل إظهار الاختلافات كما هو متوقع.
يمكنك تثبيت CSV-Compare باستخدام pip :
pip install csv-compare-toolلاستخدام CSV-Compare في مشروع Python الخاص بك ، اتبع هذه الخطوات.
مهم
عند الاستيراد كحزمة في مشروع Python ، تأكد من إزالة الواصلات حتى تقرأ الحزمة csvcomparetool (انظر PEP 8 لمزيد من المعلومات)
import csvcomparetool اختياريا ، يمكنك استيراد فئة CSVComparer وحدها
from csvcomparetool import CSVComparerCSVComparer csv1_path = "path/to/first.csv"
csv2_path = "path/to/second.csv"
column = "identifier_column"
comparer = CSVComparer ( csv1_path , csv2_path , column ) if not comparer . validate_paths ():
print ( "CSV file paths are invalid. Please check the file paths and try again." )
return if not comparer . validate_columns ():
print ( "Provided column not found in CSV. Check the columns and try again." )
return differences = comparer . find_differences () for difference in differences :
print ( f"Record ' { difference } ' is present in CSV2 but not in CSV1." ) from csvcomparetool import CSVComparer
csv1_path = "path/to/first.csv"
csv2_path = "path/to/second.csv"
column = "identifier_column"
comparer = CSVComparer ( csv1_path , csv2_path , column )
if not comparer . validate_paths () or not comparer . validate_columns ():
print ( "CSV file paths are invalid, or the column identifier does not exist. Check the file paths and columns and try again." )
else :
differences = comparer . find_differences ()
for difference in differences :
print ( f"Record ' { difference } ' is present in CSV2 but not in CSV1." )أولاً ، استنساخ هذا المستودع إلى دليل محلي على جهازك
git clone https://github.com/liquidz00/csv-compare.gitانتقل إلى موقع الريبو المستنسخ (الدليل المحلي حيث اخترت حفظ المستودع)
cd /path/to/repo/src/csvcomparetool/أخيرًا ، قم بتشغيل الأمر التالي
python cli.py /csv/path/one /csv/path/two columnidentifier