比较两个csv文件和颜色代码csv的差异

时间:2016-09-27 11:35:06

标签: python csv pandas

我想比较两个CSV文件。如果特定细胞存在差异(例如:第5行和第3列),则为该细胞提供红色。

我可以比较两个文件,但无法为我尝试此代码的差异单元格提供红色

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

我想要这样的输出: 这里红色单元格表示特定值与第一个csv单元格不相等

enter image description here

2 个答案:

答案 0 :(得分:0)

您无法在csv文件中设置颜色。希望你能做的就是在excel中做到这一点: 看看这两个问题:How to change background color of excel cell with python xlwt library?Setting a cell's fill RGB color with pywin32 in excel

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

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

对作者的信任不是我

答案 1 :(得分:0)

您只能将条件格式应用于Excel文件(例如xlsx),而不是.csv

我会按以下方式组织代码:

希望这有帮助