Python程序不写输出csv,其他一切似乎都正常工作

2024-05-29 02:34:09 发布

您现在位置:Python中文网/ 问答频道 /正文

    from subprocess import check_output
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:
    reader = csv.DictReader(f_csv)
    for row in reader:
        with open("INPUT", 'w') as f_in:
             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 = {sama]}\n".format(**row))    # for Python versions less than 3.6
             f_in.write(f"TIME = {row['sama']}\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")
          check_output('sbdart >> output1.csv',shell=True)  #slarrt is the program, and ouytput.csv is the output file

这是我的代码,在@wwii的帮助下

我的最后一行,check\u output csv根本不写入我的输出文件。有什么问题吗? 谢谢

sbdart是一个程序,它在命令行中获取输入文件和输出


Tags: csvtheinformatforoutputcheckversions
2条回答

使用提供的方法here您可以尝试使用此方法。你知道吗

import subprocess
proc = subprocess.Popen('cmd.exe', stdin = subprocess.PIPE, stdout = subprocess.PIPE)
stdout, stderr = proc.communicate('sbdart >> output.csv')

确保放置sbdart的完整路径,或导航到包含sbdart的文件夹,或将sbdart的位置添加到系统路径

在提供的链接中还有许多其他方法

使用python 3.5在Linux上工作
假设sbdart是可执行的,我们有一个名为output1.csv
sbdart在我们的测试用例中是这样的:

echo $1
echo "$(cat $1)"

output1.csv如下:

&INPUT
WLINF = 0.250
WLSUP = 4.0
WLINC = 0.5
IDAY = 289
ALAT = {row['Lat']}
ALON = {row['Long']}
IDATM = 3
ISALB = 5
IAER = 5
WLBAER = .500,.675,.870,.936,1.02
WBAER = 5*0.9
GBAER = 5*0.8
TIME = {row['sama']}
QBAER = {','.join(extinction_pct(row))}
ZOUT = 0.0,15.0
/


>>> import subprocess
>>> subprocess.check_output(['./sbdart output1.csv'],shell=True)
b"output1.csv\n&INPUT\nWLINF = 0.250\nWLSUP = 4.0\nWLINC = 0.5\nIDAY = 289\nALAT = {row['Lat']}\nALON = {row['Long']}\nIDATM = 3\nISALB = 5\nIAER = 5\nWLBAER = .500,.675,.870,.936,1.02\nWBAER = 5*0.9\nGBAER = 5*0.8\nTIME = {row['sama']}\nQBAER = {','.join(extinction_pct(row))}\nZOUT = 0.0,15.0\n/\n"
>>> 

相关问题 更多 >

    热门问题