我目前正在尝试编写一个脚本,在其中读取 Python 中的 .grb 文件并将变量保存到 .mat 文件中。我正在尝试使用 if 语句来编写每个文件的文件名,但出现语法错误。我不明白为什么会出现语法错误:
我的代码:
import xarray as xr
from scipy.io import savemat
import numpy as np
import glob
year=1990
month=1
for file in glob.glob("*.grb"):
data=xr.open_dataset(file,engine='cfgrib')
u10=data['u10'].values
u10=np.transpose(u10,[2,1,0])
#set the file name
if month < 12
filename="era_u10"+str(year)+"_0"+str(month)+".mat"
savemat(filename,{'u10':u10})
month=month+1
elif month < 12 and month > 9
filename="era_u10"+str(year)+"_"+str(month)+".mat"
savemat(filename,{'u10':u10})
month=month+1
elif month > 12
month=1
year=year+1
filename="era_u10"+str(year)+"_0"+str(month)+".mat"
savemat(filename,{'u10':u10})
我已经尝试修复缩进,但仍然出现语法错误。即使只是:
if month < 12
导致语法错误。
您的 if 语句中缺少一个冒号。
if month < 12
应该是 if month < 12: