本文介绍了将标头写入python中的excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何遍历列表中的每个元素并将其作为excel标头?让我知道是否有重复的问题.到目前为止,我找不到它.
How do you loop through each element in a list and put it as excel header? Let me know if there is a duplicate question. I couldnt find it so far.
row=0
col=0
j = 0
title = ['No.', 'Hue', 'Saturation', 'Value',
'Lightness', 'AComponent', 'BComponent',
'Blue Channel', 'Green Channel', 'Red Channel']
for i in title[0:len(title)]:
worksheet.write(row + 1, col + j, 'title[%i]', bold)
j += 1
我想做一些类似红色的文字
I wanted to do something like the text in red
推荐答案
您似乎误解了两件事:
- 要遍历整个循环,不需要范围或索引
- 要将某些外部值替换为字符串,您不能执行正在执行的操作. Python对字符串不做任何特殊的操作.
您可以将enumerate
用于j
计数器,并摆脱单独的计数器变量.
You can use enumerate
for the j
counter and get rid of a separate counter variable.
for j, t in enumerate(title):
worksheet.write(row + 1, col + j, t, bold)
免责声明:我不知道如何使用xlsxwriter
,因此就编写标头的问题而言,这可能不是最好的方法.但是,此答案的作用是解决您的错误.
Disclaimer: I do not know how to work with xlsxwriter
, so as far as the question of writing headers is concerned, this may not be the best way. However, what this answer does is address your errors.
这篇关于将标头写入python中的excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!