爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 14054|回复: 0

[求助] 用xlwt和xlrd做一个循环读写excel

[复制链接]

新浪微博达人勋

发表于 2020-6-27 15:23:37 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
读取一个txt文本,里面写了如图中格式的年月日时间等内容, 批注 2020-06-27 103016.jpg
一行中年月日是确定的,时间的个数不确定,所以想通过循环写入excel,excel表格格式也如图中,
批注 2020-06-27 1033.jpg
通过搜集大神们写的代码和自己编写,写出了如下代码(中间省去了写入数据的步骤),但是提示 return self._cell_values[rowx][colx] IndexError: list index out of range想求助一下一下该如何修改。


  1. import xlwt
  2. import xlrd

  3. f = xlwt.Workbook(encoding='utf-8')       #新建工作簿
  4. sheet1 = f.add_sheet("大雾",cell_overwrite_ok=True)          #新建sheet
  5. f.save(r'D:\forgdata\test.xlsx')   #保存


  6. #设置表格样式
  7. def set_style(name,height,bold=False):
  8.         style = xlwt.XFStyle()
  9.         font = xlwt.Font()
  10.         font.name = name
  11.         font.bold = False
  12.         font.color_index = 4
  13.         font.height = height
  14.         style.font = font
  15.         return style

  16. #写Excel
  17. import xarray as xr

  18. def print2excel():   
  19.    
  20.     lines = open("code.txt", 'r').readlines()
  21.     x = int(1)
  22.     for i in range(len(lines)):        
  23.         if i%2==0:                       #偶数行
  24.             fields = lines[i].strip('\n')
  25.             fields = fields.split(' ') #split data
  26.             year = fields[0]
  27.             month = fields[1]
  28.             day = fields[2]
  29.             row0 = ["年","月","日","时次(UTC)",'五角星','三角','方框','黑桃','备注']
  30.             for k in range(0,len(row0)):   #写第一行
  31.                 sheet1.write(0,k,row0[k],set_style('Times New Roman',220,True))   
  32.             for j in range(len(fields)):               
  33.                 read = xlrd.open_workbook(r'D:\forgdata\test.xlsx')
  34.                 sheet2 = read.sheet_by_index(0)
  35.                 cell_x = sheet2.cell_value(x,0)
  36.                 int(j)
  37.                 n = j-3   
  38.                 y = x+5*n-1
  39.                 if cell_x == 0:
  40.                  sheet1.write_merge(x,y,0,0,year)
  41.                  sheet1.write_merge(x,y,1,1,month)
  42.                  sheet1.write_merge(x,y,2,2,day)
  43.                  if j > 2:
  44.                      time=fields[j]   
  45.                      j = j-1
  46.                      sheet1.write_merge(x,x+4,3,3,time)
  47.                      x = x+5
  48.                      ds1 = xr.open_dataset('d:/era_down/CDS%s%s%s'% (year,month,day)+'h.grib',engine='cfgrib')
  49.                      r = ds1['r']
  50.                      t = ds1['t']
  51.                      ds2 = xr.open_dataset('d:/era_down/CDS%s%s%s'% (year,month,day)+'s.grib',engine='cfgrib')
  52.                  else:
  53.                      continue
  54.                 else:
  55.                     pass
  56.                 x = x+1
  57.             else:
  58.                 continue
  59.     else:
  60.         pass
  61.     f.save('D:\forgdata\test.xlsx')
  62. if __name__ == '__main__':
  63.     print2excel()   
复制代码


密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表