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't set a colour in a csv file. Want you can do is doing it in excel: 
Have a look at those two questions: <a href="http://stackoverflow.com/q/10532367/3659824">How to change background color of excel cell with python xlwt library?</a> and <a href="http://stackoverflow.com/q/11444207/3659824">Setting a cell's fill RGB color with pywin32 in excel</a></p>

<p>To summen up the <a href="http://stackoverflow.com/a/10542220/3659824">answers</a>: </p>

<pre><code>from xlwt import Workbook
import xlwt
book = Workbook()
sheet1 = book.add_sheet('Sheet 1')
book.add_sheet('Sheet 2')
for i in range(0, 100):
    st = xlwt.easyxf('pattern: pattern solid;')
    st.pattern.pattern_fore_colour = i
    sheet1.write(i % 24, i / 24, 'Test text',st)
book.save('simple.xls')
</code></pre>

<p>or <a href="http://stackoverflow.com/a/11445155/3659824">here</a>: </p>

<pre><code>def rgb_to_hex(rgb):
    strValue = '%02x%02x%02x' % rgb
    iValue = int(strValue, 16)
    return iValue

xl.ActiveSheet.Cells(row, column).interior.color = rgb_to_hex((255,255,0))
</code></pre>

<p>Credits to the authors not me </p>

python csv pandas