如何将Fortran输出写为CSV文件? [英] How to write Fortran Output as CSV file?

查看:1664
本文介绍了如何将Fortran输出写为CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人可以告诉我,我怎么能以CSV格式写我的Fortran程序的输出?所以我可以在Excel中打开CSV文件来绘制数据。

解决方案

我还会推荐来自 FLIBS 。 Fortran具备读取csv文件的能力,但不能写入它们。使用csv_file模块,您将

 使用csv_file 

在函数/子例程的开始,然后调用它:

 调用csv_write(unit,value,advance)

其中unit =文件单元号,value =数组或标量值,并提前= .true。或.false。取决于您是否要提前到下一行。



示例程序:

  program write_csv 

use csv_file

implicit none

integer :: a(3),b(2)

open(unit = 1,file ='test.txt',status ='unknown')

a =(/ 1,2,3 /)
b = / 4,5 /)

调用csv_write(1,a,.true。)
调用csv_write(1,b,.true。)

结束程序

输出:



1,2, 3



4,5



如果你只是想使用写命令,我想你必须这样做:

 写(1,'(I1,A,I1,A,I1)')a )(1),',',b,(2),(a) 

这是非常复杂的,需要你知道你的价值观的最大位数。 p>

我强烈建议使用csv_file mod ULE。这当然可以节省我很多小时的挫折。


Can any one tell me, how can I write my output of Fortran program in CSV format? So I can open the CSV file in Excel for plotting data.

解决方案

I'd also recommend the csv_file module from FLIBS. Fortran is well equipped to read csv files, but not so much to write them. With the csv_file module, you put

    use csv_file

at the beginning of your function/subroutine and then call it with:

    call csv_write(unit, value, advance)

where unit = the file unit number, value = the array or scalar value you want to write, and advance = .true. or .false. depending on whether you want to advance to the next line or not.

Sample program:

  program write_csv

    use csv_file

    implicit none

    integer :: a(3), b(2)

    open(unit=1,file='test.txt',status='unknown')

    a = (/1,2,3/)
    b = (/4,5/)

    call csv_write(1,a,.true.)
    call csv_write(1,b,.true.)

  end program

output:

1,2,3

4,5

if you instead just want to use the write command, I think you have to do it like this:

    write(1,'(I1,A,I1,A,I1)') a(1),',',a(2),',',a(3)
    write(1,'(I1,A,I1)') b(1),',',b(2)

which is very convoluted and requires you to know the maximum number of digits your values will have.

I'd strongly suggest using the csv_file module. It's certainly saved me many hours of frustration.

这篇关于如何将Fortran输出写为CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆