我有几个列表,我在python 2.7中使用xlsxwriter写入excel电子表格的不同列/行.对于一个字符串列表(DNA序列),我想在字符串中找到某些字符(‘a’,’t’,’c’,’g’),更改它们各自的颜色,然后编写完整的字符串列表(电子表格中的多个字符串,每个字符)到一列.
到目前为止,我写的代码是:
row = 1
col = 1
for i in (seqs):
worksheet.write(row,1,i,green)
for char in i:
if i.__contains__("A") or i.__contains__("T") :
worksheet.write(row,1,i[char],red)
row += 1
seqs是我的序列列表.我希望A / T为红色,G / C为绿色,并将完整序列写入电子表格.我没有收到任何错误,但我要么在excel中将每行的整个序列写成绿色,要么每行写一个红色的字符.有没有办法做这个/让这个代码工作?
解决方法:
您可以使用XlsxWriter的write_rich_string()方法执行此操作.
这是一个小工作示例:
from xlsxwriter.workbook import Workbook
workbook = Workbook('sequences.xlsx')
worksheet = workbook.add_worksheet()
red = workbook.add_format({'color': 'red'})
green = workbook.add_format({'color': 'green'})
sequences = [
'ACAAGATG',
'CCATTGTC',
'CCCCGGCC',
'CCTGCTGC',
'GCTGCTCT',
'CGGGGCCA',
'GGCCACCG',
]
worksheet.set_column('A:A', 40)
for row_num, sequence in enumerate(sequences):
format_pairs = []
# Get each DNA base character from the sequence.
for base in sequence.upper():
# Prefix each base with a format.
if base == 'A' or base == 'T':
format_pairs.extend((red, base))
elif base == 'G' or base == 'C':
format_pairs.extend((green, base))
else:
# Non base characters are unformatted.
format_pairs.append(base)
worksheet.write_rich_string(row_num, 0, *format_pairs)
workbook.close()
输出:
标签:python,colors,xlsxwriter