我知道如何從Python啓動Stata。在這裏,我有小程序從Python運行Stata並確保沒有錯誤
def dostata(dofile, *params):
## Launch a do-file, given the fullpath to the do-file
## and a list of parameters.
cmd = ["C:\Program Files (x86)\Stata13\StataMP-64.exe", "do", dofile]
for param in params:
cmd.append(param)
a = subprocess.Popen(cmd, shell=True)
path = "C:/My/do/file/dir/"
filename = "try.do"
dostata(path + filename, model, "1", "")
這是工作,或多或少。但它並不能保證Stata程序能夠成功完成。我如何從Stata獲得一些反饋,說「完成成功」?
我還沒有很好的Python編程,所以我不能肯定地說。如果不可行,我會建議將Stata輸出寫入純文本日誌文件,然後在Python中掃描該文檔以查找錯誤或返回代碼。你可以使用正則表達式來搜索r([0-9]);或類似的東西,以查找Stata輸出是否包含錯誤。 – ander2ed