我正在使用 XLWT 从 .csv 编写 excel 文件,并且我将 csv 中的第一列作为该行的样式。如何从每行的第二列开始写入值(以不打印出值,例如“headerStyle”)?我尝试了几种不同的方法,例如创建 col_count 但没有任何运气。
row_count = 0
style = rowStyle
#Read each row and write to sheet
for row in csv_input:
#Iterate through each column
for col in range(len(row)):
if col == 0:
style = row[col]
else:
if(is_number(row[col]) == True):
sheet.write(row_count,col,float(row[col]),style)
else:
sheet.write(row_count,col,row[col],style)
#Increment row_count
row_count += 1
任何帮助表示赞赏!谢谢!
我最终弄清楚了。对于任何感兴趣的人,一个问题是样式作为字符串返回,所以我创建了一个函数来解决这个问题:
def assign_style(string):
if string=='headerStyle':
style = headerStyle
return style
然后以下内容将在跳过第一列时循环:
row_count = 0
#Read each row and write to sheet
for row in csv_input:
#Iterate through each column
for col in range(len(row)):
if col == 0:
style = assign_style(row[col])
elif(is_number(row[col]) == True):
sheet.write(row_count,col-1,float(row[col]),style)
else:
sheet.write(row_count,col-1,row[col],style)
#Increment row_count
row_count += 1