|
2 | 2 |
|
3 | 3 | A modern Fortran library for reading and writing CSV (comma-separated value) files.
|
4 | 4 |
|
5 |
| -This is a work in progress, and is not finished! |
| 5 | +### Examples |
| 6 | + |
| 7 | +Everything is handled by an object-oriented `csv_file` class. Here is an example for writing a file: |
| 8 | + |
| 9 | +```fortran |
| 10 | +program csv_write_test |
| 11 | +
|
| 12 | +use csv_module |
| 13 | +use iso_fortran_env, only: wp => real64 |
| 14 | +
|
| 15 | +implicit none |
| 16 | +
|
| 17 | +type(csv_file) :: f |
| 18 | +logical :: status_ok |
| 19 | +
|
| 20 | +! open the file |
| 21 | +call f%open('test.csv',n_cols=4,status_ok=status_ok) |
| 22 | +
|
| 23 | +! add header |
| 24 | +call f%add(['x','y','z','t']) |
| 25 | +call f%next_row() |
| 26 | +
|
| 27 | +! add some data: |
| 28 | +call f%add([1.0_wp,2.0_wp,3.0_wp],real_fmt='(F5.3)') |
| 29 | +call f%add(.true.) |
| 30 | +call f%next_row() |
| 31 | +call f%add([4.0_wp,5.0_wp,6.0_wp],real_fmt='(F5.3)') |
| 32 | +call f%add(.false.) |
| 33 | +call f%next_row() |
| 34 | +
|
| 35 | +! finished |
| 36 | +call f%close(status_ok) |
| 37 | +
|
| 38 | +end program csv_write_test |
| 39 | +``` |
| 40 | + |
| 41 | +Which produces the following file: |
| 42 | +``` |
| 43 | +x,y,z,t |
| 44 | +1.000,2.000,3.000,T |
| 45 | +4.000,5.000,6.000,F |
| 46 | +``` |
| 47 | + |
| 48 | +Real, integer, logical, or character data can be added as scalars, vectors, and matrices. |
| 49 | + |
| 50 | +When reading a CSV file, the data is stored internally in the class as allocatable character strings, which can be retrieved as real, integer, logical or character vectors as necessary. For example, to get the `x`, `y`, `z`, and `t` vectors from the previously-generated file: |
| 51 | + |
| 52 | +```fortran |
| 53 | +program csv_read_test |
| 54 | +
|
| 55 | +use csv_module |
| 56 | +use iso_fortran_env, only: wp => real64 |
| 57 | +
|
| 58 | +implicit none |
| 59 | +
|
| 60 | +type(csv_file) :: f |
| 61 | +character(len=30),dimension(:),allocatable :: header |
| 62 | +real(wp),dimension(:),allocatable :: x,y,z |
| 63 | +logical,dimension(:),allocatable :: t |
| 64 | +logical :: status_ok |
| 65 | +integer,dimension(:),allocatable :: itypes |
| 66 | +
|
| 67 | +! read the file |
| 68 | +call f%read('test.csv',header_row=1,status_ok=status_ok) |
| 69 | +
|
| 70 | +! get the header and type info |
| 71 | +call f%get_header(header,status_ok) |
| 72 | +call f%variable_types(itypes,status_ok) |
| 73 | +
|
| 74 | +! get some data |
| 75 | +call f%get(1,x,status_ok) |
| 76 | +call f%get(2,y,status_ok) |
| 77 | +call f%get(3,z,status_ok) |
| 78 | +call f%get(4,t,status_ok) |
| 79 | +
|
| 80 | +! destroy the file |
| 81 | +call f%destroy() |
| 82 | +
|
| 83 | +end program csv_read_test |
| 84 | +``` |
| 85 | + |
| 86 | +Various options are user-selectable for specifying the format (e.g., changing the quote or delimiter characters). You can choose to enclose strings (or all fields) in quotes or not. The library works pretty well, and there are probably additional improvements that could be made. For one thing, it doesn't properly handle the case of a string that contains the delimiter character (I'll eventually fix this). If anybody has any other improvements, fork it and send me a pull request. |
6 | 87 |
|
7 | 88 | ### License
|
8 | 89 |
|
|
0 commit comments