-1

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.

I can able to compare two files but unable to give red color to the difference cell I have tried this 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()

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

enter image description here

Stivan
  • 1,128
  • 1
  • 15
  • 24
user3153140
  • 59
  • 2
  • 8

2 Answers2

0

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: How to change background color of excel cell with python xlwt library? and Setting a cell's fill RGB color with pywin32 in excel

To summen up the answers:

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')

or here:

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))

Credits to the authors not me

Community
  • 1
  • 1
LaughU
  • 253
  • 2
  • 11
  • 22
0

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

I would organise the code in the following way:

Hope this helps

FLab
  • 7,136
  • 5
  • 36
  • 69