如何将数据写入TXT文件中的特定位置?

发布于 2024-12-14 14:56:46 字数 237 浏览 2 评论 0原文

我想更改文本文件的一部分。文件内容如下:

PLANE
CV = 1.4, PRINT = 1
Ks = 3.17, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
PLANE

我想用随机数替换 ks=3.17 部分。更具体地说,文本文件是另一个可执行文件的输入,我想修改Ks值,运行可执行文件,再次修改Ks值,等等。

I want to change part of a text file. The file's contents follow:

PLANE
CV = 1.4, PRINT = 1
Ks = 3.17, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
PLANE

I want to replace the ks=3.17 part with a random number. More specifically, the text file is the input to another executable, and I want to modify the Ks value, run the executable, modify the Ks value again, and so on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

调妓 2024-12-21 14:56:46

可能有更好的方法可以做到这一点,但这是有效的。它写入一个新的输出文件,将 Ks 值更改为 9.99。

Program modify_value

implicit none

character(len=100) :: row
integer :: j
real :: Ks

Ks = 9.99

open(unit=1,file='data.txt',status='old')
open(unit=2,file='data_modified.txt',status='unknown')

do j=1,4
  read(1,'(a)') row
  if (j==3) then
    write(2,'(a,F5.2,a)') row(1:4), Ks, trim(row(10:))
  else
    write(2,'(a)') trim(row)
  end if
end do

close(1)
close(2)

End Program

There are probably better ways of doing it, but this works. It writes a new output file changing the Ks value to 9.99.

Program modify_value

implicit none

character(len=100) :: row
integer :: j
real :: Ks

Ks = 9.99

open(unit=1,file='data.txt',status='old')
open(unit=2,file='data_modified.txt',status='unknown')

do j=1,4
  read(1,'(a)') row
  if (j==3) then
    write(2,'(a,F5.2,a)') row(1:4), Ks, trim(row(10:))
  else
    write(2,'(a)') trim(row)
  end if
end do

close(1)
close(2)

End Program
疯到世界奔溃 2024-12-21 14:56:46

您有充分的理由使用 Fortran 来完成这项任务吗?您在上一段中描述的任务可以使用其他语言轻松完成。例如,如果您编写如下所示的 python 脚本,

import os, random

# repeat 10 times
for i in range(10):

    # generate random Ks with Normal distribution, mean=3.17, stdev=1
    ks = random.normalvariate(3.17,1)

    # write input file
    with open('inp.txt','w') as f:

        f.write( 
"""PLANE
CV = 1.4, PRINT = 1
Ks = %4.2f, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
PLANE""" % ks )


    # run the program
    os.system("a.exe")

给定这样的示例 fortran 程序,

program test
  character(len=80) :: line
  open(10,file='inp.txt',status='old')
  do i=1,4
  read(10,'(a)') line
  write(*,*) trim(line)
  enddo
end program

您将得到

$ python test.py
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 1.21, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 3.08, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 4.55, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 4.10, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 2.24, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 4.62, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 2.76, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 2.69, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 4.58, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 3.32, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE

Do you have strong reason to use fortran for this task? The task you described in the last paragraph may be accomplished with other languages with less hustle. For example if you write a python script like below,

import os, random

# repeat 10 times
for i in range(10):

    # generate random Ks with Normal distribution, mean=3.17, stdev=1
    ks = random.normalvariate(3.17,1)

    # write input file
    with open('inp.txt','w') as f:

        f.write( 
"""PLANE
CV = 1.4, PRINT = 1
Ks = %4.2f, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
PLANE""" % ks )


    # run the program
    os.system("a.exe")

Given sample fortran program like this,

program test
  character(len=80) :: line
  open(10,file='inp.txt',status='old')
  do i=1,4
  read(10,'(a)') line
  write(*,*) trim(line)
  enddo
end program

You will get

$ python test.py
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 1.21, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 3.08, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 4.55, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 4.10, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 2.24, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 4.62, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 2.76, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 2.69, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 4.58, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE
 PLANE
 CV = 1.4, PRINT = 1
 Ks = 3.32, G = 120.67, DIST = 0.34, POR = 0.456, ROCK = 0.3
 PLANE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文