【问题标题】:Compare two csv files and color code the difference in csv比较两个 csv 文件并对 csv 中的差异进行颜色编码
【发布时间】:2016-09-27 11:35:06
【问题描述】:

我想比较两个 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 单元格

【问题讨论】:

  • 您想为“CSV”文件添加颜色吗?这是不可能的。

标签: python csv pandas


【解决方案1】:

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

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

希望对你有帮助

【讨论】:

    【解决方案2】:

    您不能在 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))
    

    感谢作者而不是我

    【讨论】:

      猜你喜欢
      • 2014-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-17
      • 2016-12-24
      • 1970-01-01
      相关资源
      最近更新 更多