Python Forum
Compare fields from two csv files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare fields from two csv files
#3
I never have anything to do with large amounts of data. My longest csv is only about 200 lines long.

Don't know how you could compare each line without calling each line!

Maybe I have not understood what you really want.

def myApp():
    # read in 2 big old .csv files (in my case tiny)
    path2csv1 = '/home/pedro/myPython/csv/csv1.csv'
    with open(path2csv1) as CSV1:
        list1 = CSV1.readlines()

    path2csv2 = '/home/pedro/myPython/csv/csv2.csv'
    with open(path2csv2) as CSV2:
        list2 = CSV2.readlines()        

    # first problem: the lists are not equal in length?
    if not len(list1) == len(list2):
        print('Problem, the lists are not the same length ... ')
    
    difference = 0   
    if len(list1) > len(list2):
        difference = len(list1) - len(list2)
    if len(list2) > len(list1):
        difference = len(list2) - len(list1)

    dlist = []
    for d in range(0, difference):
        dlist.append('X')

    if len(list1) > len(list2):
        list2 = list2 + dlist
    if len(list2) > len(list1):
        list1 = list1 + dlist   

    # check lengths
    if not len(list1) == len(list2):
        print('Problem, the lists are not the same length ... ')
    elif len(list1) == len(list2):
        print('The lists now have the same length, we can proceed ... '

    # generate a list of tuples of dissimilar lines    
    def getDisimilar_lines():
        for i in range(len(list1)):
            if list1[i] != list2[i]:
                yield (list1[i], list2[i], f'line_{i}')

    not_the_same = getDisimilar_lines()

    for t in not_the_same:
            print(t)    
    # easy to write to Excel from here
Reply


Messages In This Thread
Compare fields from two csv files - by georgebijum - Apr-23-2022, 04:19 AM
RE: Compare fields from two csv files - by DPaul - Apr-23-2022, 07:37 AM
RE: Compare fields from two csv files - by Pedroski55 - Apr-25-2022, 07:27 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 728 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Compare 2 files tslavov 2 1,053 Feb-12-2023, 10:53 AM
Last Post: ibreeden
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,717 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  Json fields not being retrieved mrcurious2020 4 2,137 Sep-14-2020, 06:24 AM
Last Post: bowlofred
  How can I compare Python XML-Files and add missing values from one to another kirat 2 2,847 Aug-30-2019, 12:17 PM
Last Post: perfringo
  Compare two large CSV files for a match Python_Newbie9 3 5,918 Apr-22-2019, 08:49 PM
Last Post: ichabod801
  Keyword compare in two files and output the results kotigasp 2 2,903 Jan-23-2018, 08:13 AM
Last Post: buran
  Mapping Fields zak308 0 2,547 Jan-09-2018, 10:02 PM
Last Post: zak308

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020