[Python] How to compare two CSV data and write into new CSV file

Discussão em 'Python' iniciado por Stack, Agosto 20, 2025.

  1. Stack

    Stack Membro Participativo

    I have two .csv files, master_test.csv and child_test1.csv and want to get output.csv.

    master_test.csv contains the following information

    nnn
    bbb_12
    kkk
    ccc


    child_test1.csv contains the following information:

    nnn
    ddd
    mmm
    bbb_13


    I need to compare the child_test1.csv data to master_test.csv file, and finally write to output.csv file.

    I need output to be like this:

    nnn nnn Match found
    ddd --- Not found Master sheet
    mmm ---- Not found Master sheet
    bbb_13 bbb_12 Not Match or Version change


    I have tried the code shown here. I'm able to achieve match found in the same column, but I need out as above output.csv file

    After running the code, I got this output:

    nnn
    match nnn


    but I need output as below:

    nnn nnn Match found
    ddd --- Not found Master sheet
    mmm ---- Not found Master sheet
    bbb_13 bbb_12 Not Match or Version change


    Please can anyone guide me?

    f1 = open("C:\\Python34\\master_test.csv", "r")
    f2 = open("C:\\Python34\\child_test1.csv", "r")
    outFile = open("C:\\Python34\\output.csv", "w")

    fileOne = f1.readlines()
    fileTwo = f2.readlines()

    f1.close()
    f2.close()
    for i in fileOne:
    for x in fileTwo:
    if (x == i):
    outFile.write(i +"Equal " + x)

    Continue reading...

Compartilhe esta Página