首页 > 解决方案 > Python程序不写入输出csv,其他一切似乎都正常工作

问题描述

    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_output csv 根本没有写入我的输出文件。可能是什么问题?谢谢

sbdart 是一个程序,它接受 INPUT 文件并在命令行中输出

标签: python

解决方案


使用此处提供的方法,您可以尝试使用它。

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

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

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


推荐阅读