0

I am new at python and need help in comparing two TXT files and outputting their difference. Here is the scenario:

These text files contains the structure of a database table. We usually update the database and make structural changes to it, so basically we will have a pair of TXT files -- (for example) an Employee.txt file (before the DB upgrade in a folder like C:\before_upgrade) and a Employee.txt file (after the upgrade in a folder like C:\after_upgrade) containing the structure of the Employee Table. Employee.txt will contain the Column_Name, DataType, IsNullable, ColumnLength values like:

Emp_Id, bigint, No, null
Emp_F_Name, varchar, Yes, 100
Emp_L_Name, varchar, Yes, 200
IsActive, smallint, No, null

Now, I need to compare these TXT files and basically output the difference in such a way that it more more understandable to the user. Like if for a column, the IsNullable changes from Yes to No, then we output something like

"for Column_name: Emp_L_Name the IsNullable field changed to NO"

OR if a new column is added

"New column XYZ got added"

OR if a column is dropped

"Column IsActive got dropped" etc.

There are several tables in the DB so we need to compare more than one text file pairs. The table names will be read from another text file (like table_names.txt).

Thanks in advance.

P_Ar
  • 377
  • 2
  • 9
  • 25
  • Perhaps the [difflib module](https://kite.com/python/examples/3986/difflib-generate-a-diff-between-two-text-files). Examples of usage: [comparing bodies of text](https://pymotw.com/2/difflib/) – DarrylG Apr 30 '20 at 20:01
  • Since this is from a database table is there an index column? There doesn't seem to be one in your example data. – DarrylG Apr 30 '20 at 23:39

1 Answers1

0

hope your doing well!

For checking the columns, assuming the first row is the column names you could read that from the file, split on commas and convert to a set. Then just use set difference to discover what got dropped and what got added.

For checking the rows, read both files line by line and split on commas. Then for each common column check if the value was changed.

Hope it helps! Stay safe!

André Alves
  • 139
  • 7