python 处理 csv file 变色

读csv文件,把符合条件的行/列变色:

例如:如果mylist里的item在第一列中,把那一行highlight

import csv

# 读取csv至字典
csvFile = open("instance.csv", "r")
reader = csv.reader(csvFile)

# 建立空字典
result = {}
for item in reader:
    result[item[0]] = item

csvFile.close()
print(result)

mylist = ['miaomiao', 'cat']

import xlsxwriter

with xlsxwriter.Workbook('instance.xlsx') as workbook:
    worksheet = workbook.add_worksheet()

    data_format1 = workbook.add_format({'bg_color': '#FFC7CE'})
    for row, key in enumerate(result.keys()):
        for name in mylist:
            if name in key:
                worksheet.set_row(row, cell_format=data_format1)
                break
        for col, data in enumerate(result[key]):
            try:
                data = int(data)
            except:
                pass
            worksheet.write(row, col, data)


 

你可能感兴趣的:(python,python)