C应该返回Python call()什么?

2024-05-15 04:03:46 发布

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

继续我在Executing a C program in python?上的上一个问题;在Python中,什么从C返回一个来获得可用的数据??在

当前我的程序返回:

int main (int argc, char *argv[])
{
    spa_data spa;  //declare the SPA structure
    int result;
    float min, sec;

    //enter required input values into SPA structure

    spa.year          = 2003;
    spa.month         = 10;
    spa.day           = 17;
    spa.hour          = 12;
    spa.minute        = 30;
    spa.second        = 30;
    spa.timezone      = -7.0;
    spa.delta_t       = 67;
    spa.longitude     = -105.1786;
    spa.latitude      = 39.742476;
    spa.elevation     = 1830.14;
    spa.pressure      = 820;
    spa.temperature   = 11;
    spa.slope         = 30;
    spa.azm_rotation  = -10;
    spa.atmos_refract = 0.5667;
    spa.function      = SPA_ALL;

    //call the SPA calculate function and pass the SPA structure

    result = spa_calculate(&spa);

    if (result == 0)  //check for SPA errors
    {
        //display the results inside the SPA structure

        printf("Julian Day:    %.6f\n",spa.jd);
        printf("L:             %.6e degrees\n",spa.l);
        printf("B:             %.6e degrees\n",spa.b);
        printf("R:             %.6f AU\n",spa.r);
        printf("H:             %.6f degrees\n",spa.h);
        printf("Delta Psi:     %.6e degrees\n",spa.del_psi);
        printf("Delta Epsilon: %.6e degrees\n",spa.del_epsilon);
        printf("Epsilon:       %.6f degrees\n",spa.epsilon);
        printf("Zenith:        %.6f degrees\n",spa.zenith);
        printf("Azimuth:       %.6f degrees\n",spa.azimuth);
        printf("Incidence:     %.6f degrees\n",spa.incidence);

        min = 60.0*(spa.sunrise - (int)(spa.sunrise));
        sec = 60.0*(min - (int)min);
        printf("Sunrise:       %02d:%02d:%02d Local Time\n", (int)(spa.sunrise), (int)min, (int)sec);

        min = 60.0*(spa.sunset - (int)(spa.sunset));
        sec = 60.0*(min - (int)min);
        printf("Sunset:        %02d:%02d:%02d Local Time\n", (int)(spa.sunset), (int)min, (int)sec);

    } else printf("SPA Error Code: %d\n", result);

    return 0;
}

我读了一些关于structs和Pythons包的文章,但是我还不能完全理解它,所以也许有人可以指出正确的方向。在


Tags: thefunctionsecresultminstructureintdelta
1条回答
网友
1楼 · 发布于 2024-05-15 04:03:46

将数据返回到Python的最简单方法是以某种合理的格式打印出来。你有一个不错的,但一个简单的CSV会更容易一些。在

然后使用subprocess.Popen

p = subprocess.Popen(["./spa", "args", "to", "spa"], stdout=subprocess.PIPE)
(stdout, stderr) = p.communicate()
data = parse_output(stdout.read())

例如,如果输出是CSV:

^{pr2}$

parse_output可以写入:

^{3}$

现在,这确实提出了一系列假设……具体来说:

  • 您正在处理的数据数量很少(Popen.communicate()将所有输出存储在内存中,然后将其返回到程序)
  • 你不会经常调用./spa(产生一个进程非常非常慢)

但如果可以的话,这对你很有用。在

相关问题 更多 >

    热门问题