我的目标是从 fortran 中的 python numpy 数组打印第二个字符串,但我只打印第一个字符,而且它也不一定是正确的字符串。
谁能告诉我将完整字符串数组传递给 Fortran 的正确方法是什么?
代码如下:
测试py
import numpy as np
import testa4
strvar = np.asarray(['aa','bb','cc'], dtype = np.dtype('a2'))
testa4.testa4(strvar)
特斯塔4.f90
subroutine testa4(strvar)
implicit none
character(len=2), intent(in) :: strvar(3)
!character*2 does not work here - why?
print *, strvar(2)
end subroutine testa4
编译为
f2py -c -m testa4 testa4.f90
上面代码的输出
c
所需输出
bb
我不知道如何使用f2py
。但这可以用ctypes
。您将获得一个字符数组,但您可以非常轻松地将其转换为字符串。
subroutine testa4(strvar) bind(C, name='testa4')
use iso_c_binding
implicit none
character(len=1,kind=c_char), intent(in) :: strvar(2,3)
print *, strvar(:,2)
end subroutine testa4
编译:gfortran -shared -fPIC testa4.f90 -o testa4.so
import numpy as np
import ctypes
testa4 = ctypes.CDLL("./testa4.so")
strvar = np.asarray(['aa','bb','cc'], dtype = np.dtype('a2'))
strvar_p = ctypes.c_void_p(strvar.ctypes.data)
testa4.testa4(strvar_p)
run:
> python testpy.f90
bb
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)