我对FORTRAN的经验有限,我需要解析具有以下结构的文件:
def function(list, string):
returns [(word, position in the string)]
我需要搜索标签(例如H s 13.010000 0.019685
1.962000 0.137977
0.444600 0.478148
s 0.122000 1.000000
p 0.727000 1.000000
***
He s 38.360000 0.023809
5.770000 0.154891
1.240000 0.469987
s 0.297600 1.000000
p 1.275000 1.000000
***
),然后将相应的块读入数组。
我知道我可以通过指定每行应该具有的格式来解析文件,但是这里有不同的格式。 在Python中,我只是将每行用空格分开,然后根据列数进行处理。但是如何在FORTRAN中解决这个问题?
答案 0 :(得分:2)
您可以将每一行作为字符串读取,然后进行处理。如果看起来格式是固定的(前两个字符中的元素符号,第六个字符中的轨道字母等),则以下程序可以为您提供启发:
program elms
implicit none
integer, parameter :: MAX_LEN = 40
character(len=MAX_LEN) :: line_el, line
integer :: u
integer :: is
integer :: nlin
character(len=2) :: element = 'He'
integer, parameter :: MAX_LINES = 20
real, dimension(MAX_LINES) :: e, f
open(newunit=u, file='elms.dat', status='old', action='read')
main_loop: do
! Read line
read(u, '(a)', iostat=is) line_el
if (eof_iostat(is)) exit main_loop
! Check first two characters of the line vs. chemical element.
if (line_el(1:2) .eq. element) then
! This is the beginning of an element block
nlin = 0
line = line_el
do
if (line .ne. '') then
! Line is not empty or only spaces.
nlin = nlin + 1
if (line(6:6) .ne. ' ') then
! Line contains an orbital letter - process it.
end if
! Read the real values in the rest of the line
read(line(7:),*) e(nlin), f(nlin)
end if
! Read next line
read(u, '(a)', iostat=is) line
if (eof_iostat(is)) exit main_loop
if (line(1:2) .ne. ' ') then
! Finished processing element block.
exit main_loop
end if
end do
end if
end do main_loop
! Close file
close(u)
contains
logical function eof_iostat(istat)
! Returns true if the end of file has been reached
use, intrinsic :: iso_fortran_env, only: IOSTAT_END
implicit none
integer, intent(in) :: istat
select case (istat)
case (0) ! No error
eof_iostat = .false.
case (IOSTAT_END) ! End of file reached
eof_iostat = .true.
case default ! Error
STOP
end select
end function eof_iostat
end program
您可能需要使程序成为子例程,将element
设为intent(in)
伪参数,处理轨道符号,等等。
请注意,如果可能的话,一次性读取文件中的所有数据,然后在内存中搜索相关数据(例如,具有化学符号的数组)会更容易。
>