Python Excel从第二列开始写csv的值

我使用XLWT从.csv编写excel文件,并将csv中的第一列作为行的样式。 我怎样才能开始写每行的第二列开始的值(如不打印出的值,例如,“headerStyle”)? 我已经尝试了几种不同的方式,例如创build一个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 

任何帮助表示赞赏! 谢谢!

我终于搞清楚了。 对于任何感兴趣的人来说,一个问题就是风格是以stringforms回来的,所以我创build了一个函数来解决这个问题

 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 

使用iter() 。 另外,不要遍历range() 。 而是使用enumerate() 。 并使用三元运算符,它有助于保持干燥:

 for row_count, row in enumerate(csv_input): columns = iter(row) style = next(columns) for col_count, col in enumerate(columns, start=1): sheet.write( row_count, col_count, float(col) if is_number(col) else col, style )