簡體   English   中英

Python程序未寫入輸出csv,其他所有內容似乎均正常運行

[英]Python program doesn't write to output csv, everything else seems to work correctly

    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文件並在命令行中輸出

您可以嘗試使用此處提供的方法。

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"
>>> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM