Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

gen a python script to check two csv to have the different record with code

  1. User

    gen a python script to check two csv to have the different record with code

  2. 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 output_path with the respective paths to your CSV files.

    Note: This script assumes that both CSV files have the same structure (i.e., same number of columns and same column names in the same order). If it's not the case, you might need to modify the logic in the compare_csv() function to correctly identify the differences between files.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.