You should use an excel file for this, because csv files are merely plain text files that cannot keep any coloring formatting within them. Basically, the only time a CSV file may seem like it's got formatting is when it's opened in Excel.
In any case, my recommendation is that you try using the xlsxwriter
package for this. You can install it with an easy pip install XlsxWriter
.
I have created a sample script for you to get you started. You will notice that I have a line that creates a formatting: bold with font-size
red. That formatting is only used when a cell value (cell_data
) is equal to "xyz"
.
from collections import OrderedDict
import xlsxwriter
data = {"1":["xyz",""],"2":["abc","def"],"3":["zzz",""]}
# Use an OrderedDict to maintain the order of the columns
data = OrderedDict((k,data.get(k)) for k in sorted(data.keys()))
# Open an Excel workbook
workbook = xlsxwriter.Workbook('dict_to_excel.xlsx')
# Set up a format
book_format = workbook.add_format(properties={'bold': True, 'font_color': 'red'})
# Create a sheet
worksheet = workbook.add_worksheet('dict_data')
# Write the headers
for col_num, header in enumerate(data.keys()):
worksheet.write(0,col_num, int(header))
# Save the data from the OrderedDict into the excel sheet
for row_num,row_data in enumerate(zip(*data.values())):
for col_num, cell_data in enumerate(row_data):
if cell_data == "xyz":
worksheet.write(row_num+1, col_num, cell_data, book_format)
else:
worksheet.write(row_num+1, col_num, cell_data)
# Close the workbook
workbook.close()
You should get:
I hope this helps.