如何将Fortran输出写入CSV文件?

时间:2021-05-17 14:07:04

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.

有谁能告诉我,我如何用CSV格式写出Fortran程序的输出?我可以用Excel打开CSV文件来绘制数据。

4 个解决方案

#1


9  

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

我还推荐来自FLIBS的csv_file模块。Fortran具备读取csv文件的能力,但并没有那么多的内容来编写它们。使用csv_file模块,您可以放置

    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.

其中unit =文件单元号,value =要写入的数组或标量值,advance = .true。或.false。取决于你是否想要进入下一行。

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

1、2、3

4,5

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.

我强烈建议使用csv_file模块。这无疑为我节省了很多时间的挫败感。

#2


10  

A slightly simpler version of the write statement could be:

写语句的稍微简单一点的版本可以是:

write (1, '(1x, F, 3(",", F))') a(1), a(2), a(3), a(4)

Of course, this only works if your data is numeric or easily repeatable. You can leave the formatting to your spreadsheet program or be more explicit here.

当然,这只在数据是数字的或容易重复的情况下才有效。您可以将格式保留到电子表格程序中,或者在这里更显式一些。

#3


2  

Tens seconds work with a search engine finds me the FLIBS library, which includes a module called csv_file which will write strings, scalars and arrays out is CSV format.

几十秒的搜索引擎找到我的FLIBS库,它包括一个叫做csv_file的模块,它会把字符串、标量和数组写成CSV格式。

#4


2  

Here is what I'm using (works with G95):

以下是我使用的(与G95一起使用):

write(unit,'(999(G21.6,:,","))')array or data structure containing number or character variables

The Intel compiler recognizes

英特尔编译器识别

write(unit,'(*(G0.6,:,","))')array or data structure

which doesn't have excess blanks, and the line can have more than 999 columns.

它没有多余的空格,行可以有999多个列。

To remove excess blanks with F95, first write into a character buffer and then use your own CSV_write program to take out the excess blanks, like this:

要删除F95的多余空格,首先写入一个字符缓冲区,然后使用自己的CSV_write程序取出多余的空格,如下所示:

write(Buf,'(999(G21.6,:,","))')array or data structure
call CSV_write(unit,Buf)

You can also use

您还可以使用

write(Buf,*)array or data structure
call CSV_write(unit,Buf)

where your CSV_write program replaces whitespace with "," in Buf. This is problematic in that it doesn't separate character variables unless there are extra blanks (i.e. 'a ','abc ' is OK).

在Buf中,您的CSV_write程序将用“,”替换空白。这是有问题的,因为它没有分离字符变量,除非有额外的空格(例如。“a”、“abc”都可以。

#1


9  

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

我还推荐来自FLIBS的csv_file模块。Fortran具备读取csv文件的能力,但并没有那么多的内容来编写它们。使用csv_file模块,您可以放置

    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.

其中unit =文件单元号,value =要写入的数组或标量值,advance = .true。或.false。取决于你是否想要进入下一行。

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

1、2、3

4,5

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.

我强烈建议使用csv_file模块。这无疑为我节省了很多时间的挫败感。

#2


10  

A slightly simpler version of the write statement could be:

写语句的稍微简单一点的版本可以是:

write (1, '(1x, F, 3(",", F))') a(1), a(2), a(3), a(4)

Of course, this only works if your data is numeric or easily repeatable. You can leave the formatting to your spreadsheet program or be more explicit here.

当然,这只在数据是数字的或容易重复的情况下才有效。您可以将格式保留到电子表格程序中,或者在这里更显式一些。

#3


2  

Tens seconds work with a search engine finds me the FLIBS library, which includes a module called csv_file which will write strings, scalars and arrays out is CSV format.

几十秒的搜索引擎找到我的FLIBS库,它包括一个叫做csv_file的模块,它会把字符串、标量和数组写成CSV格式。

#4


2  

Here is what I'm using (works with G95):

以下是我使用的(与G95一起使用):

write(unit,'(999(G21.6,:,","))')array or data structure containing number or character variables

The Intel compiler recognizes

英特尔编译器识别

write(unit,'(*(G0.6,:,","))')array or data structure

which doesn't have excess blanks, and the line can have more than 999 columns.

它没有多余的空格,行可以有999多个列。

To remove excess blanks with F95, first write into a character buffer and then use your own CSV_write program to take out the excess blanks, like this:

要删除F95的多余空格,首先写入一个字符缓冲区,然后使用自己的CSV_write程序取出多余的空格,如下所示:

write(Buf,'(999(G21.6,:,","))')array or data structure
call CSV_write(unit,Buf)

You can also use

您还可以使用

write(Buf,*)array or data structure
call CSV_write(unit,Buf)

where your CSV_write program replaces whitespace with "," in Buf. This is problematic in that it doesn't separate character variables unless there are extra blanks (i.e. 'a ','abc ' is OK).

在Buf中,您的CSV_write程序将用“,”替换空白。这是有问题的,因为它没有分离字符变量,除非有额外的空格(例如。“a”、“abc”都可以。