0

I am not a developer, do some python coding for the ease of my work. The new project i am working on is to have two excel files, compare them and generate changes. for the files, we will call them as Old File and New File. Both files got fixed columns but row may vary. in both files, most of the data are identical in same row and column, but some data may vary. say two sample columns from each files are

Old File                                New File

id    item                              id     item
-------------------                     -----------------------------
1     apple, banana                     1      banana, grape
2     grape                             2      apple
3     orange

I have read the files using nested list. For example old file data would be like

old_file_list = [ ['1','apple,banana'], ['2','grape'], ['3','orange'] ]
new_file_list = [ ['1','banana,grape'], ['2','apple'] ]

need to generate following output based on the data above,

print output

item      old_file_id    new_file_id    status
----------------------------------------------
apple     1                2           changed
orange    3                -           deleted
grape     2                1           changed

Please guide me how to achieve this, many thanks

Mauro Baraldi
  • 6,346
  • 2
  • 32
  • 43
  • You could iterate over the **old_list** and then, for each item in **old_list**, check if this item is equal in **new_list**. The result of check, could be stored in na result list, and then print each result item. – Mauro Baraldi Jun 12 '18 at 02:17

1 Answers1

0

DISCLAIMER: Since you said you're not a developer, I will present a pretty simple, and easy to comprehend way to find your solution. It may not be the most efficient way to do that. THIS IS NOT THE SOLUTION. But a guide to help find solution.

This is a classical problem of iterate through nested lists.

There is a lot of information missing, that may invalidate this solution, but it's is just a guidance.

For each item in old_file list, you need to check if this item is exactly like in new_file list, and print unchanged items.

for item in old_file_list:
    if item in new_file_list:
        print 'Item %r unchanged' % item

Next step is check subitems of old_file list is in any sub list of new_list. I will assume that the sublists has only 2 items.

    else:
        for new_item in new_file_list:
            if item[1] in new_item:
                print("Item %r changed" % item[1])

If old_lis sub item isn't in no one new_list sub item, we may assume that it's deleted.

            else:
                print("Item %r deleted" % item[1])

Nevertheless the item may be in different order, but I will not handle this condition.

An example of this code, with your data is

>>> old_file_list = [ ['1','apple,banana'], ['2','grape'], ['3','orange'] ]
>>> new_file_list = [ ['1','banana,grape'], ['2','apple'] ]
>>>
>>> for item in old_file_list:
...     if item in new_file_list:
...         print('Item %r unchanged' % item)
...     else:
...         for new_item in new_file_list:
...             if item[1] in new_item:
...                 print("Item %r changed" % item[1])
...             else:
...                 print("Item %r deleted" % item[1])
...
Item 'apple,banana' deleted
Item 'apple,banana' deleted
Item 'grape' deleted
Item 'grape' deleted
Item 'orange' deleted
Item 'orange' deleted

It isn't the result you expect, but from here I hope that you can find your solution.

Mauro Baraldi
  • 6,346
  • 2
  • 32
  • 43