写一个循环读写excel

读取一个txt文本,文本的格式如下,只列出了部分数据,一天当中整点时刻的个数是不确定的


然后把这些年月日时间写入一个excel表中,格式如下


我自己写的代码老是会报IndexError: list index out of range的错误,代码中省略了数据输出的部分我的代码如下,请大神们帮忙
import xlwt
import xlrd
import xlutils.copy


#设置表格样式
def set_style(name,height,bold=False):
	style = xlwt.XFStyle()
	font = xlwt.Font()
	font.name = name
	font.bold = False
	font.color_index = 4
	font.height = height
	style.font = font
	return style


f = xlwt.Workbook(encoding='utf-8')       #新建工作簿
sheet1 = f.add_sheet("大雾",cell_overwrite_ok=True)          #新建sheet
f.save(r'D:\forgdata\test.xlsx')   #保存
Workbook = xlrd.open_workbook('D:\\forgdata\\test.xlsx')     #不用\\会报路径错误
sheet2 = Workbook.sheet_by_name('大雾')
workbook_xlutils = xlutils.copy.copy(Workbook)  #使用xlrd打开一个已存在的xls,并通过xlutils复制
x = int(1)








#写Excel
import xarray as xr


def print2excel():    
    
    lines = open("code.txt", 'r').readlines()
    sheet1 = workbook_xlutils.get_sheet(0)  #通过sheet序号获取sheet
    x = int(1)
    for i in range(len(lines)):        
        if i%2==0:                       #偶数行
            fields = lines[i].strip('\n')
            fields = fields.split(' ') #split data
            year = fields[0]
            month = fields[1]
            day = fields[2]
            alignment = xlwt.Alignment()
            alignment.horz = xlwt.Alignment.HORZ_CENTER
            alignment.vert = xlwt.Alignment.VERT_CENTER
            sheet1.col(0).width = 256*10
            sheet1.col(1).width = 256*10
            sheet1.col(2).width = 256*10
            sheet1.col(3).width = 256*10
            sheet1.col(8).width = 256*20
            row0 = ["年","月","日","时次(UTC)",'五角星','三角','方框','黑桃','备注']
            #写第一行
            for k in range(0,len(row0)):
                sheet1.write(0,k,row0[k],set_style('Times New Roman',220,True))    
            for j in range(len(fields)):                
                int(j)
                n = int(j)-3    
                y = x+5*n-1
                if (sheet2.cell(1,0).ctype) == 0:
                 sheet1.write_merge(x,y,0,0,year)
                 sheet1.write_merge(x,y,1,1,month)
                 sheet1.write_merge(x,y,2,2,day)
                 if j > 2:
                     time=fields[j]   #读到的第J个元素加到list()中
                     j = j-1
                     sheet1.write_merge(x,y,3,3,time)
                     ds1 = xr.open_dataset('d:/era_down/CDS%s%s%s'% (year,month,day)+'h.grib',engine='cfgrib')
                     r = ds1['r']
                     t = ds1['t']
                     ds2 = xr.open_dataset('d:/era_down/CDS%s%s%s'% (year,month,day)+'s.grib',engine='cfgrib')
                     ts = ds2['t2m']                                                 #1000相对湿度
                     datar = r.loc['%s-%s-%sT%s'% (year,month,day,time)].loc[1000]    #loc函数查找定位时间,高度层      
                     a1 = datar.interp(latitude=27.98,longitude=121.02)          #interp函数插值计算
                     a2 = float(a1)                                             #转为float格式 
                     a3 = round(a2,2)                                           #取两位小数,round函数非简单四舍五入
                     sheet1.write(x,4,a3)                           #输出到文本必须为字符格式           
                     b1 = datar.interp(latitude=27.92,longitude=120.86)     
                     b2 = float(b1)                                      
                     b3 = round(b2,2)                                   
                     sheet1.write(x,5,b3)
                     c1 = datar.interp(latitude=27.92,longitude=120.96)     
                     c2 = float(c1)                                      
                     c3 = round(c2,2)                                   
                     sheet1.write(x,6,c3)
                     d1 = datar.interp(latitude=27.82,longitude=120.9)     
                     d2 = float(d1)                                      
                     d3 = round(d2,2)                                   
                     sheet1.write(x,7,d3)
                     sheet1.write(x,8,'1000hPa相对湿度')                                                                                                                                          
                 else:
                     continue
                else:
                    pass
                x = x+1
            else:
                continue
    else:
        pass
    workbook_xlutils.save('D:\forgdata\test.xlsx')
if __name__ == '__main__':
    print2excel()    

能否把你原数据发部分给我1467288927@qq.com


确定这个\f和\t不报错?【doge】

你这个怕是有难度啊,还缺文件


https://biwen.csdn.net/question/4345

https://biwen.csdn.net/question/4345

https://www.cnblogs.com/dlight/p/biwen.html

具体是报错信息贴出来看看



我是想sheet2.cell_value为0的时候,就是为单元格空的时候写入年月日,这样用不对应该如何修改呢?


你这两个部分是有些问题的,第二部分其实是不需要的,直接处理sheet1,如添加sheet、写单元格……,最后save即可。

而sheet2是只读的


因为需要下载很多数据然后输出到单元格,就是保存一次后,过几天还需要在这个文件写入,所以才设置了第二部分