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 ให้แน่ใจว่าได้ ลบ Hyphens เพื่อให้แพคเกจอ่าน 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นำทางไปยังตำแหน่ง repo ที่ถูกโคลน (ไดเรกทอรีท้องถิ่นที่คุณเลือกที่จะบันทึกที่เก็บ)
cd /path/to/repo/src/csvcomparetool/สุดท้ายเรียกใช้คำสั่งต่อไปนี้
python cli.py /csv/path/one /csv/path/two columnidentifier