gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
gen a python script to check two csv to have the different record with code
- User
gen a python script to check two csv to have the different record with code
- Assistant
Below is a Python script that checks two CSV files for different records and generates a new CSV file with the different records. Please customize the file paths for your specific files.
import csv file1_path = 'file1.csv' file2_path = 'file2.csv' output_path = 'differences.csv' def read_csv(file_path): with open(file_path, 'r') as file: reader = csv.reader(file) data = [row for row in reader] return data def compare_csv(data1, data2): differences = [] for row in data1: if row not in data2: differences.append(row) return differences def write_csv(file_path, data): with open(file_path, 'w', newline='') as file: writer = csv.writer(file) writer.writerows(data) if __name__ == '__main__': data1 = read_csv(file1_path) data2 = read_csv(file2_path) differences = compare_csv(data1, data2) write_csv(output_path, differences) print(f'Different records saved to: {output_path}')Replace the
file1_path,file2_path, and with the respective paths to your CSV files.