Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Fortran输出读入Python?_Python_Python 3.x_Fortran - Fatal编程技术网

如何将Fortran输出读入Python?

如何将Fortran输出读入Python?,python,python-3.x,fortran,Python,Python 3.x,Fortran,我继承了如下代码: Python -> File -> Modern Fortran -> File -> Python 其中每个文件都包含一个简单的实数数组 我现在需要运行这个程序很多次,I/O正在伤害我。我想省略这些文件,将Python输出读入Fortran,并将Fortran输出读回Python 通过从Python调用Fortran例程并将real作为一系列字符串参数提供,可以省略第一个文件 ## This Python script converts a to

我继承了如下代码:

Python -> File -> Modern Fortran -> File -> Python
其中每个文件都包含一个简单的实数数组

我现在需要运行这个程序很多次,I/O正在伤害我。我想省略这些文件,将Python输出读入Fortran,并将Fortran输出读回Python

通过从Python调用Fortran例程并将real作为一系列字符串参数提供,可以省略第一个文件

## This Python script converts a to a string and provides it as 
## an argument to the Fortran script test_arg

import subprocess

a = 3.123456789
status = subprocess.call("./test_arg " + str(a), shell=True)

如果不使用中间文件而将变量“b”输出到另一个Python脚本中,工作代码段会是什么样子


我读过关于f2py的书,但是将继承的Fortan脚本转换成Python模块所涉及的重构量远远超过了我想要做的

如果可以将Fortran代码重建为库,那么可以通过多种方式使用Python中的库

  • 使用。例如,请参见中的示例
  • 使用。参见例如和

如果可以将Fortran代码重建为库,那么可以通过多种方式使用Python中的库

  • 使用。例如,请参见中的示例
  • 使用。参见例如和

我发现适合我需要的是:

## Sends a0 as a string to fortran script.
## Receives stdout from fortran, decodes it from binary to ascii,
## splits up values, and converts to a numpy array.

from subprocess import *
from decimal import Decimal as Dec

a0 = 3.123456789

proc = subprocess.Popen(["./test_arg", str(Dec(a0))], stdout=subprocess.PIPE)
out, err= proc.communicate()
result = np.array(out.decode('ascii').split(), dtype=float)

我发现适合我需要的是:

## Sends a0 as a string to fortran script.
## Receives stdout from fortran, decodes it from binary to ascii,
## splits up values, and converts to a numpy array.

from subprocess import *
from decimal import Decimal as Dec

a0 = 3.123456789

proc = subprocess.Popen(["./test_arg", str(Dec(a0))], stdout=subprocess.PIPE)
out, err= proc.communicate()
result = np.array(out.decode('ascii').split(), dtype=float)

不要创建Fortran程序,创建一个子例程。或者创建一个子例程而不是主程序作为Fortran代码大部分的接口。您可以让Fortran将其输出写入标准输出(
write(*,*)b
),并且您应该能够通过子进程读取结果。fortran需要是“干净的”,而不是向标准输出写任何东西。(实际上集成代码更好,但这很简单)我只想补充一点,使用
shell=True
时要小心,它会带来风险。在调用fortran脚本的subprocess命令下面,我可以放置另一个从fortran的write(,)读取stdout的subprocess命令吗?您能提供一个最低限度的工作示例吗?从进程读取:。要摆脱shell,您可以将参数作为列表而不是字符串传递。不要创建Fortran程序,请创建一个子例程。或者创建一个子例程而不是主程序作为Fortran代码大部分的接口。您可以让Fortran将其输出写入标准输出(
write(*,*)b
),并且您应该能够通过子进程读取结果。fortran需要是“干净的”,而不是向标准输出写任何东西。(实际上集成代码更好,但这很简单)我只想补充一点,使用
shell=True
时要小心,它会带来风险。在调用fortran脚本的subprocess命令下面,我可以放置另一个从fortran的write(,)读取stdout的subprocess命令吗?您能提供一个最低限度的工作示例吗?从进程读取:。为了摆脱shell,您将参数作为列表传递,而不是一个字符串。或者。。。但首先他必须从程序转到子程序。或者。。。但首先他必须从程序转到子程序。