【问题标题】:How to run a Stata do-file from Python in MacOS如何在 MacOS 中从 Python 运行 Stata do 文件
【发布时间】:2020-06-21 12:51:07
【问题描述】:

我是 Python 新手,正在尝试从 Python 执行 Stata do-file,使用 MacOS

以下问题的建议:Run Stata do file from Python

我尝试了以下代码:

dofile = folder_path + "stata_dofile.do"
cmd = ["C:/Program Files (x86)/Stata14/StataSE-64", "do", dofile, variable1, variable2]
subprocess.call(cmd) 

其中“dofile”是我想要执行的 Stata do 文件,“variable1”和“variable2”是我想要提供给 Stata 的一些字符串变量。

此代码在 Windows 上运行良好。但是当我尝试按如下方式使其适应 Mac 时:

dofile = folder_path + "stata_dofile.do"
cmd = ["/Applications/Stata/StataMP.app", "do", dofile, variable1, variable2]
subprocess.call(cmd) 

我收到一条错误消息“权限被拒绝”。

根据这个论坛上的一些其他建议,我确实设法使用以下代码在我的 Mac 上从 Python 打开了 Stata 应用程序:

cmd = ["/usr/bin/open", "/Applications/Stata/StataMP.app"]
subprocess.call(cmd)

但这只是打开Stata,我不知道如何修改代码使其执行上面的do-file。

基本上我不知道自己在做什么。非常感谢任何提示!

【问题讨论】:

    标签: python stata


    【解决方案1】:

    我想通了!

    假设我们在桌面上有一个test.do 文件,如下所示:

    sysuse auto, clear
    
    fsum price mpg, s(mean median sd max min) f(%12.3f)
    
    

    我们不应该使用StataSE.app,而是应该打开它的包内容:

    import subprocess
    
    cmd = ["/Applications/Stata/StataSE.app/Contents/MacOS/StataSE", "do", "/Users/apple/Desktop/test.do"]
    
    subprocess.call(cmd) 
    

    我希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 2014-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 2021-05-02
      相关资源
      最近更新 更多