Section: Write CSV Files#

In this section, we will write CSV files in Fortran using the CSV_Fortran Module. The CSV_Fortran Module is a Fortran module that provides a simple interface for reading and writing CSV files. The module is available at jacobwilliams/csv-fortran. Documentation for the module is available at https://jacobwilliams.github.io/csv-fortran/.

Example Programs#

In the documentation for the CSV_Fortran Module, there are several example programs that demonstrate how to read and write CSV files. We will use the example programs to demonstrate how to write CSV files in Fortran.

Writing a CSV File#

The following example program writes a CSV file named test.csv:

program csv_write_test

  use csv_module
    use iso_fortran_env, only: wp => real64

    implicit none

    type(csv_file) :: f
    logical :: status_ok

    ! set optional inputs:
    call f%initialize(verbose = .true.)

    ! open the file
    call f%open('test.csv',n_cols=4,status_ok=status_ok)

    ! add header
    call f%add(['x','y','z','t'])
    call f%next_row()

    ! add some data:
    call f%add([1.0_wp,2.0_wp,3.0_wp],real_fmt='(F5.3)')
    call f%add(.true.)
    call f%next_row()
    call f%add([4.0_wp,5.0_wp,6.0_wp],real_fmt='(F5.3)')
    call f%add(.false.)
    call f%next_row()

    ! finished
    call f%close(status_ok)
  
end program csv_write_test

The program writes a CSV file named test.csv with the following contents:

x,y,z,t
1.000,2.000,3.000,T
4.000,5.000,6.000,F

Building the Example Program using FPM (Fortran Package Manager)#

TOML File#

The above program was compiled using the Fortran Package Manager (FPM). The following is the fpm.toml file used to build the program:

name = "Section_CSV_Fortran_Write_CSV"

[build]
auto-executables = true
auto-tests = true
auto-examples = true

[install]
library = false

[dependencies]
csv-fortran = { git="https://github.com/jacobwilliams/csv-fortran.git" }

[[executable]]
name="Section_CSV_Fortran_Write_CSV"
source-dir="app"
main="section_csv_fortran_write_csv.f90"

Build the Program#

import os
root_dir = ""
root_dir = os.getcwd()
code_dir = root_dir + "/" + "Fortran_Code/Section_CSV_Fortran_Write_CSV"
os.chdir(code_dir)
build_status = os.system("fpm build 2>/dev/null")

Run the Program#

The program is run and the output is saved into a file named ‘data.dat

exec_status = \
    os.system("fpm run 2>/dev/null")

Examine the Output File#

!cat test.csv
"x","y","z","t"
1.000,2.000,3.000,T
4.000,5.000,6.000,F