Compare two csv files and color code the difference in csv
<p>I want to compare two CSV files. If there is difference in a particular cell (Ex: 5th row and 3rd column) then give red color to that cell.</p>

<p>I can able to compare two files but unable to give red color to the difference cell I have tried this code</p>

<pre><code>def compare():
try:
    assert_frame_equal(df_sort_sas, df_sort_py)
    return True
except:  # appeantly AssertionError doesn't catch all
    return False 
compare()
</code></pre>

<p>I want output like this:
Here red colored cell means that particular value is not equal with first csv cell </p>

<p><a href="http://i.stack.imgur.com/Cwyqq.png" rel="nofollow"><img src="http://i.stack.imgur.com/Cwyqq.png" alt="enter image description here"></a></p>

<p>You can only apply conditional formatting to excel files (e.g. xlsx), not .csv</p>

<p>I would organise the code in the following way:</p>

<ul>
<li>Use pandas to compare the two csv and create a dataframe with a flag for the differences</li>
<li>Use xlsx module to write a xlsx file, coloring in red the cells where you have mismatch  (<a href="http://xlsxwriter.readthedocs.io/example_conditional_format.html?highlight=conditional%20formatting" rel="nofollow">http://xlsxwriter.readthedocs.io/example_conditional_format.html?highlight=conditional%20formatting</a>)</li>
</ul>

<p>Hope this helps</p>

python csv pandas