已知温度用python计算饱和水汽压结果写入excel

发布时间 2023-03-28 15:07:25作者: 蓝色大螃蟹

数据大致长这样

 

 想计算饱和水汽压,得先计算最高气温对应的饱和水汽压,再计算最低气温对应的饱和水汽压,两个相加除以2

 

先算最高温度对应的饱和水汽压代码如下:

 1 import xlrd
 2 import math
 3 import xlwt
 4 
 5 worksheet = xlrd.open_workbook('G:/drought/processdata/Temperature.xlsx')
 6 sheet_names= worksheet.sheet_names()
 7 print(sheet_names)
 8 for sheet_name in sheet_names:
 9     sheet = worksheet.sheet_by_name(sheet_name)
10     rows = sheet.nrows # 获取行数
11     cols = sheet.ncols # 获取列数
12     all_content = []
13 
14 
15     cols = sheet.col_values(6)[1:] # 获取第二列内容,后面[1:]是把第一行字符串内容切出去,从第二行开始读
16 
17     def e0(cols):
18         print(cols)
19         l = []
20         for i in cols:
21             e = float(0.6108 * (math.e ** ((17.27 * i / (i + 237.3)))))
22             l.append(e)
23         return l
24 
25 
26 
27 emax = e0(cols)
28 print(emax)
29 
30 book = xlwt.Workbook(encoding='utf-8',style_compression=0) #创建一个excel表格类型文件
31 sheet = book.add_sheet('最高温度饱和水汽压',cell_overwrite_ok=True) #创建sheet
32 
33 sheet.write(0,0,'最高温度饱和水汽压') #填标题
34 
35 for i in range(len(emax)): #弄个循环放数据
36     sheet.write(i+1,0,emax[i]) #第一列值
37 
38 savepath = 'G:/drought/processdata/highbaohee.xls'
39 book.save(savepath)