我是一名学生,正在处理这个古怪的旧程序,该程序仅将文件作为输入并直接输出到终端。
我有一个python脚本,该脚本创建了该输入文件,并将输出写入文本文件。但是,每次运行时,我都必须自己在python脚本中手动更改变量。有五个变量实例,所有实例均采用恒定格式。我可以循环/排列吗?
我的python脚本:
CoreMatcher
此脚本
获取需要输入的这些值,将它们写入输入文件,运行其他程序(从输入文件中获取输入),然后将输出写入csv文件。然后我更改脚本中的值。
每次运行都需要更改变量ALAT,ALON,QBAER和TIME,这些值在excel表中。无论如何,我可以一次完成这些操作吗?太好了,我一直在手动编辑每一行。
数据是这里的样子
答案 0 :(得分:0)
将电子表格导出为csv文件;使用csv reader遍历行;使用行值更新您的变量。
假设csv具有列标题,并且QBAER
只是AOT xxxx
字段的内容:
import csv, operator
extinction_pct = operator.itemgetter('AOT 500','AOT 675','AOT 870','AOT 936','AOT 1020')
with open('csv_export.csv') as f_csv, open("INPUT", 'w') as f_in:
reader = csv.DictReader(f_csv)
for row in reader:
f_in.write("&INPUT\n")
f_in.write("WLINF = 0.250\n") #lower frequency value
f_in.write("WLSUP = 4.0\n") #highest frequency value
f_in.write("WLINC = 0.5\n") #wavelength increment
f_in.write("IDAY = 289\n") #computing for a specific day
#f_in.write("ALAT = {Lat}\n".format(**row)) # for Python versions less than 3.6
f_in.write(f"ALAT = {row['Lat']}\n") #latitude of the location
#f_in.write("ALON = {Long}\n".format(**row)) # for Python versions less than 3.6
f_in.write(f"ALON = {row['Long']}\n") #longitude of the location
f_in.write("IDATM = 3\n") #atmopsheric model 2 - mid latitude summer
f_in.write("ISALB = 5\n") #surface albedo feature
f_in.write("IAER = 5\n") #boundary layer aerosol type selection - 5 - user defined spectral dependance of BLA
f_in.write("WLBAER = .500,.675,.870,.936,1.02\n") #wavelenght points for IAER
f_in.write("WBAER = 5*0.9\n") #single scattering albedo
f_in.write("GBAER = 5*0.8\n") #assymetric factor used with IAER
#f_in.write("TIME = {Time]}\n".format(**row)) # for Python versions less than 3.6
f_in.write(f"TIME = {row['Time']}\n") #Time in IST format (-5.30hr)
#f_in.write("QBAER = {}\n".format(','.join(extinction_pct(row))) # for Python versions less than 3.6
f_in.write(f"QBAER = {','.join(extinction_pct(row))}\n") #extinction efficiency percentage
f_in.write("ZOUT = 0.0,15.0\n") #TOA defining
f_in.write("/\n")
使用f-strings,该版本需要Python版本3.6以上的版本。
从这个问题尚不清楚,Time
和QBAER
字段是如何从数据派生的。
有些软件包可以直接与excel文件一起使用,但是过程相同。
如果要为每行数据创建一个新文件并检查它,请在读取器循环内移动文件 creation 。为了使内容更具可读性,我将文本构造放入了函数中。
def file_text(row):
# String concatenation isn't the most efficient but it does preserve the comment annotations
s = ''
s += "&INPUT\n"
s += "WLINF = 0.250\n" #lower frequency value
s += "WLSUP = 4.0\n" #highest frequency value
s += "WLINC = 0.5\n" #wavelength increment
s += "IDAY = 289\n" #computing for a specific day
#s += "ALAT = {Lat}\n".format(**row) # for Python versions less than 3.6
s += f"ALAT = {row['Lat']}\n" #latitude of the location
#s += "ALON = {Long}\n".format(**row) # for Python versions less than 3.6
s += f"ALON = {row['Long']}\n" #longitude of the location
s += "IDATM = 3\n" #atmopsheric model 2 - mid latitude summer
s += "ISALB = 5\n" #surface albedo feature
s += "IAER = 5\n" #boundary layer aerosol type selection - 5 - user defined spectral dependance of BLA
s += "WLBAER = .500,.675,.870,.936,1.02\n" #wavelenght points for IAER
s += "WBAER = 5*0.9\n" #single scattering albedo
s += "GBAER = 5*0.8\n" #assymetric factor used with IAER
#s += "TIME = {Time]}\n".format(**row) # for Python versions less than 3.6
s += f"TIME = {row['Time']}\n" #Time in IST format (-5.30hr)
#s += "QBAER = {}\n".format(','.join(extinction_pct(row))) # for Python versions less than 3.6
s += f"QBAER = {','.join(extinction_pct(row))}\n" #extinction efficiency percentage
s += "ZOUT = 0.0,15.0\n" #TOA defining
s += "/\n"
return s
with open('csv_export.csv') as f_csv:
reader = csv.DictReader(f_csv)
for row in reader:
with open("INPUT", 'w') as f_in:
f_in.write(file_text(row))
check_output('slarrt >> output1.csv',shell=True) #slarrt is the program, and ouytput.csv is the output file