0
我想從文件夾中讀取許多grib文件,然後另存爲文本文件。 文本文件的名稱與grib文件名稱相同。 我的代碼:使用Python將grib文件轉換爲文本文件
import pygrib, os, glob
LAT1 = None
LAT2 = None
LON1 = None
LON2 = None
def process_message(grb, outfile):
tmps = "%s" % grb
wtitle = tmps.split(':')[1]
wtime = tmps.split(':')[len(tmps.split(':'))-1].split(' ')[1]
data, lat, lons = grb.data(LAT1, LAT2, LON1, LON2)
for i in xrange(len(data)):
tdata = data[i]
tlat = lat[i]
tlon = lons[1]
for j in xrange(len(tdata)):
wdata = tdata[j]
wlat = tlat[j]
wlon = tlon[j]
outfile.write("%s, %s, %s, %s, %s\n" % (wtitle, wtime, wlat,
wlon, wdata))
os.chdir('/home/george/mona_modificat/data')
filenames = glob.glob('*.grb')
for f in filenames:
grbs = pygrib.open(f)
grbs.seek(0)
outfile = f
for grb in grbs:
process_message(grb, outfile)
os.rename(outfile, outfile + '.txt')
grbs.close()
當我運行代碼,我得到這個錯誤: AttributeError的: '海峽' 對象沒有屬性 '寫'
乾杯!
現在,當我運行代碼時,它沒有錯誤,但沒有任何反應......沒有創建文本文件.... – user1032066