使用 Do-Loop 读取一列数据(数字和字符串)并将数字过滤为输出到另一个文件中

发布于 2024-10-31 17:33:00 字数 631 浏览 0 评论 0原文

我有一个输出文件,单列,其中每个第七行是一个字符串,其他行是数字(如下所示)

998.69733    
377.29340    
142.22397    
53.198547    
19.743515    
7.5493960    
timestep:  1
998.69733    
377.29340    
142.22047    
53.188023    
19.755905    
7.5060229    
timestep:  2
998.69733    
377.29337

我需要将此数据读入另一个文件,省略文本并仅保留数字,并尝试循环来分配我的字符串的虚拟,但我收到错误,因为它无法识别(AI)。

DO 10 I = 1, 1000
 IF (MOD(I,7) == 0) THEN 
  READ (8, FMT= '(AI)') dummy
 END IF
  READ (8,*) val
  WRITE (9,*) val
10 CONTINUE

(8 - 输入文件和 9 - 输出文件分配)

我对 Fortran 很陌生,花了很多时间寻找解决方案或至少是类似的问题,但没有找到任何东西。我真的很感激一些帮助。

预先非常感谢您。

I have an output file, single column, where each 7th line is a string and the others numerical (something like below)

998.69733    
377.29340    
142.22397    
53.198547    
19.743515    
7.5493960    
timestep:  1
998.69733    
377.29340    
142.22047    
53.188023    
19.755905    
7.5060229    
timestep:  2
998.69733    
377.29337

I need to read this data into another file, omitting the text and keeping only the numbers and tried a loop to allocate a dummy for my string but I get an error as it does not recognize (AI).

DO 10 I = 1, 1000
 IF (MOD(I,7) == 0) THEN 
  READ (8, FMT= '(AI)') dummy
 END IF
  READ (8,*) val
  WRITE (9,*) val
10 CONTINUE

(8 - input file and 9 - output file allocation)

I am quite new to Fortran and spent a lot of time surfing for a solution or at least a similar problem but did not find anything. I would really appreciate some help.

Thank you very much in advance.

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

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

发布评论

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

评论(2

别再吹冷风 2024-11-07 17:33:00

如果您只想跳过第七行,您可以执行“read (8, '(A)' ) dummy”,其中 dummy 被声明为字符串(即“character (len=80) :: dummy”)。有些字符是字母而其他字符是数字并不重要。

PS 现代的循环编写方式是“do”、“end do”……不需要行号和 continue 语句。

If you just want to skip the seventh lines, you could do "read (8, '(A)' ) dummy" where dummy is declared as a character string (i.e., "character (len=80) :: dummy"). It won't matter than some of the characters are letters and others numbers.

P.S. The modern was to write loops is "do", "end do" ... no need for line numbers and continue statements.

一梦等七年七年为一梦 2024-11-07 17:33:00

只需使用带有空输入项列表的列表定向输入即可。
另外,第七行在循环中被读取两次。将 val 的读写放在 ELSE 部分中,或者使用 CYCLE 语句:

DO I = 1, 1000
  IF(MOD(I,7) == 0) THEN 
    READ(8,*)
    CYCLE
  END IF
  READ(8,*) val
  WRITE(9,*) val
END DO

Simply use list-directed input with an empty input item list.
Also, the seventh line gets read twice in your loop. Put the read and write of val in an ELSE section, or, alternatively, us the CYCLE statement:

DO I = 1, 1000
  IF(MOD(I,7) == 0) THEN 
    READ(8,*)
    CYCLE
  END IF
  READ(8,*) val
  WRITE(9,*) val
END DO
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文