the main class for reading and writing CSV files.
Note
A CSV file is assumed to contain the same number of columns in each row. It may optionally contain a header row.
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
logical, | private | :: | verbose | = | .false. |
to print error messages |
|
character(len=1), | private | :: | quote | = | '"' |
quotation character |
|
character(len=1), | private | :: | delimiter | = | ',' |
delimiter character |
|
integer, | private | :: | n_rows | = | 0 |
number of rows in the file |
|
integer, | private | :: | n_cols | = | 0 |
number of columns in the file |
|
integer, | private | :: | chunk_size | = | 1024 |
for expanding vectors |
|
type(csv_string), | private, | dimension(:), allocatable | :: | header |
the header |
||
type(csv_string), | private, | dimension(:,:), allocatable | :: | csv_data |
the data in the file |
||
integer, | private | :: | icol | = | 0 |
last column written in current row |
|
integer, | private | :: | iunit | = | 0 |
file unit for writing |
|
logical, | private | :: | enclose_strings_in_quotes | = | .true. |
if true, all string cells will be enclosed in quotes. |
|
logical, | private | :: | enclose_all_in_quotes | = | .false. |
if true, all cells will be enclosed in quotes. |
|
character(len=1), | private | :: | logical_true_string | = | 'T' |
when writing a logical |
|
character(len=1), | private | :: | logical_false_string | = | 'F' |
when writing a logical |
Initialize a csv_file.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(out) | :: | me | |||
character(len=1), | intent(in), | optional | :: | quote |
(Default is |
|
character(len=1), | intent(in), | optional | :: | delimiter |
(Default is |
|
logical, | intent(in), | optional | :: | enclose_strings_in_quotes |
if true, all string cells will be enclosed in quotes. (Default is True) |
|
logical, | intent(in), | optional | :: | enclose_all_in_quotes |
if true, all cells will be enclosed in quotes. (Default is False) |
|
character(len=1), | intent(in), | optional | :: | logical_true_string |
when writing a logical |
|
character(len=1), | intent(in), | optional | :: | logical_false_string |
when writing a logical |
|
integer, | intent(in), | optional | :: | chunk_size |
factor for expanding vectors (default is 100) |
|
logical, | intent(in), | optional | :: | verbose |
print error messages to the console (default is False) |
Read a CSV file.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
character(len=*), | intent(in) | :: | filename |
the CSV file to open |
||
integer, | intent(in), | optional | :: | header_row |
the header row |
|
integer, | intent(in), | optional, | dimension(:) | :: | skip_rows |
rows to skip |
logical, | intent(out) | :: | status_ok |
status flag |
||
character(len=1), | intent(in), | optional | :: | delimiter |
(Default is |
Destroy the data in a CSV file.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(out) | :: | me |
Returns an array indicating the variable type of each columns.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
integer, | intent(out), | dimension(:), allocatable | :: | itypes | ||
logical, | intent(out) | :: | status_ok |
Returns the header as a character(len=*)
array.
(read
must have already been called to read the file).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
character(len=*), | intent(out), | dimension(:), allocatable | :: | header | ||
logical, | intent(out) | :: | status_ok |
Returns the header as a type(csv_string)
array.
(read
must have already been called to read the file).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
type(csv_string), | intent(out), | dimension(:), allocatable | :: | header | ||
logical, | intent(out) | :: | status_ok |
Returns the header as a character(len=*)
array.
(read
must have already been called to read the file).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
character(len=*), | intent(out), | dimension(:), allocatable | :: | header | ||
logical, | intent(out) | :: | status_ok |
Returns the header as a type(csv_string)
array.
(read
must have already been called to read the file).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
type(csv_string), | intent(out), | dimension(:), allocatable | :: | header | ||
logical, | intent(out) | :: | status_ok |
For getting data from the class after the file has been read.
Returns a character(len=*)
array containing the csv data
(read
must have already been called to read the file).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
character(len=*), | intent(out), | dimension(:,:), allocatable | :: | csv_data |
the data |
|
logical, | intent(out) | :: | status_ok |
status flag |
Get an individual value from the csv_data
structure in the CSV class.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | row |
row number |
||
integer, | intent(in) | :: | col |
column number |
||
class(*), | intent(out) | :: | val |
the returned value |
||
logical, | intent(out) | :: | status_ok |
status flag |
Return a column from a CSV file as a real(sp)
vector.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | icol |
column number |
||
real(kind=sp), | intent(out), | dimension(:), allocatable | :: | r | ||
logical, | intent(out) | :: | status_ok |
Return a column from a CSV file as a real(wp)
vector.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | icol |
column number |
||
real(kind=wp), | intent(out), | dimension(:), allocatable | :: | r | ||
logical, | intent(out) | :: | status_ok |
Return a column from a CSV file as a integer(ip)
vector.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | icol |
column number |
||
integer(kind=ip), | intent(out), | dimension(:), allocatable | :: | r | ||
logical, | intent(out) | :: | status_ok |
Convert a column from a csv_string
matrix to a logical
vector.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | icol |
column number |
||
logical, | intent(out), | dimension(:), allocatable | :: | r | ||
logical, | intent(out) | :: | status_ok |
Convert a column from a csv_string
matrix to a character(len=*)
vector.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | icol |
column number |
||
character(len=*), | intent(out), | dimension(:), allocatable | :: | r | ||
logical, | intent(out) | :: | status_ok |
Convert a column from a csv_string
matrix to a type(csv_string)
vector.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | icol |
column number |
||
type(csv_string), | intent(out), | dimension(:), allocatable | :: | r | ||
logical, | intent(out) | :: | status_ok |
Returns a character(len=*)
array containing the csv data
(read
must have already been called to read the file).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
character(len=*), | intent(out), | dimension(:,:), allocatable | :: | csv_data |
the data |
|
logical, | intent(out) | :: | status_ok |
status flag |
Get an individual value from the csv_data
structure in the CSV class.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | row |
row number |
||
integer, | intent(in) | :: | col |
column number |
||
class(*), | intent(out) | :: | val |
the returned value |
||
logical, | intent(out) | :: | status_ok |
status flag |
Return a column from a CSV file as a real(sp)
vector.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | icol |
column number |
||
real(kind=sp), | intent(out), | dimension(:), allocatable | :: | r | ||
logical, | intent(out) | :: | status_ok |
Return a column from a CSV file as a real(wp)
vector.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | icol |
column number |
||
real(kind=wp), | intent(out), | dimension(:), allocatable | :: | r | ||
logical, | intent(out) | :: | status_ok |
Return a column from a CSV file as a integer(ip)
vector.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | icol |
column number |
||
integer(kind=ip), | intent(out), | dimension(:), allocatable | :: | r | ||
logical, | intent(out) | :: | status_ok |
Convert a column from a csv_string
matrix to a logical
vector.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | icol |
column number |
||
logical, | intent(out), | dimension(:), allocatable | :: | r | ||
logical, | intent(out) | :: | status_ok |
Convert a column from a csv_string
matrix to a character(len=*)
vector.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | icol |
column number |
||
character(len=*), | intent(out), | dimension(:), allocatable | :: | r | ||
logical, | intent(out) | :: | status_ok |
Convert a column from a csv_string
matrix to a type(csv_string)
vector.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | icol |
column number |
||
type(csv_string), | intent(out), | dimension(:), allocatable | :: | r | ||
logical, | intent(out) | :: | status_ok |
Open a CSV file for writing.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
character(len=*), | intent(in) | :: | filename |
the CSV file to open |
||
integer, | intent(in) | :: | n_cols |
number of columns in the file |
||
logical, | intent(out) | :: | status_ok |
status flag |
||
logical, | intent(in), | optional | :: | append |
append if file exists |
Add a cell to a CSV file.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
class(*), | intent(in) | :: | val |
the value to add |
||
character(len=*), | intent(in), | optional | :: | int_fmt |
if |
|
character(len=*), | intent(in), | optional | :: | real_fmt |
if |
|
logical, | intent(in), | optional | :: | trim_str |
if |
Add a vector to a CSV file. Each element is added as a cell to the current line.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
class(*), | intent(in), | dimension(:) | :: | val |
the values to add |
|
character(len=*), | intent(in), | optional | :: | int_fmt |
if |
|
character(len=*), | intent(in), | optional | :: | real_fmt |
if |
|
logical, | intent(in), | optional | :: | trim_str |
if |
Add a matrix to a CSV file. Each row is added as a new line.
Line breaks are added at the end of each line (in this way it
differs from the other add
routines).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
class(*), | intent(in), | dimension(:,:) | :: | val |
the values to add |
|
character(len=*), | intent(in), | optional | :: | int_fmt |
if |
|
character(len=*), | intent(in), | optional | :: | real_fmt |
if |
|
logical, | intent(in), | optional | :: | trim_str |
if |
Add a cell to a CSV file.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
class(*), | intent(in) | :: | val |
the value to add |
||
character(len=*), | intent(in), | optional | :: | int_fmt |
if |
|
character(len=*), | intent(in), | optional | :: | real_fmt |
if |
|
logical, | intent(in), | optional | :: | trim_str |
if |
Add a vector to a CSV file. Each element is added as a cell to the current line.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
class(*), | intent(in), | dimension(:) | :: | val |
the values to add |
|
character(len=*), | intent(in), | optional | :: | int_fmt |
if |
|
character(len=*), | intent(in), | optional | :: | real_fmt |
if |
|
logical, | intent(in), | optional | :: | trim_str |
if |
Add a matrix to a CSV file. Each row is added as a new line.
Line breaks are added at the end of each line (in this way it
differs from the other add
routines).
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
class(*), | intent(in), | dimension(:,:) | :: | val |
the values to add |
|
character(len=*), | intent(in), | optional | :: | int_fmt |
if |
|
character(len=*), | intent(in), | optional | :: | real_fmt |
if |
|
logical, | intent(in), | optional | :: | trim_str |
if |
Close a CSV file after writing
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
logical, | intent(out) | :: | status_ok |
status flag |
Tokenize a line from a CSV file. The result is an array of csv_string
types.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
character(len=*), | intent(in) | :: | line | |||
type(csv_string), | intent(out), | dimension(:), allocatable | :: | cells |
Reads the next line from a file.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(in) | :: | me | |||
integer, | intent(in) | :: | iunit | |||
character(len=:), | intent(out), | allocatable | :: | line | ||
logical, | intent(out) | :: | status_ok |
true if no problems |
Return a column from a CSV file vector.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(csv_file), | intent(inout) | :: | me | |||
integer, | intent(in) | :: | icol |
column number |
||
class(*), | intent(out), | dimension(:) | :: | r |
assumed to have been allocated to
the correct size by the caller.
( |
|
logical, | intent(out) | :: | status_ok |
status flag |
type,public :: csv_file !! the main class for reading and writing CSV files. !! !!@note A CSV file is assumed to contain the same number !! of columns in each row. It may optionally contain !! a header row. private logical :: verbose = .false. !! to print error messages character(len=1) :: quote = '"' !! quotation character character(len=1) :: delimiter = ',' !! delimiter character ! for reading a csv file: integer :: n_rows = 0 !! number of rows in the file integer :: n_cols = 0 !! number of columns in the file integer :: chunk_size = 1024 !! for expanding vectors type(csv_string),dimension(:),allocatable :: header !! the header type(csv_string),dimension(:,:),allocatable :: csv_data !! the data in the file ! for writing a csv file: integer :: icol = 0 !! last column written in current row integer :: iunit = 0 !! file unit for writing logical :: enclose_strings_in_quotes = .true. !! if true, all string cells !! will be enclosed in quotes. logical :: enclose_all_in_quotes = .false. !! if true, *all* cells will !! be enclosed in quotes. character(len=1) :: logical_true_string = 'T' !! when writing a logical `true` !! value to a CSV file, this !! is the string to use !! (default is `T`) character(len=1) :: logical_false_string = 'F' !! when writing a logical `false` !! value to a CSV file, this !! is the string to use !! (default is `F`) contains private procedure,public :: initialize => initialize_csv_file procedure,public :: read => read_csv_file procedure,public :: destroy => destroy_csv_file procedure,public :: variable_types generic,public :: get_header => get_header_str,& get_header_csv_str procedure :: get_header_str procedure :: get_header_csv_str !> ! For getting data from the class ! after the file has been read. generic,public :: get => get_csv_data_as_str,& csv_get_value,& get_real_sp_column,& get_real_wp_column,& get_integer_column,& get_logical_column,& get_character_column,& get_csv_string_column procedure :: get_csv_data_as_str procedure :: csv_get_value procedure :: get_real_sp_column procedure :: get_real_wp_column procedure :: get_integer_column procedure :: get_logical_column procedure :: get_character_column procedure :: get_csv_string_column procedure,public :: open => open_csv_file generic,public :: add => add_cell,& add_vector,& add_matrix procedure :: add_cell procedure :: add_vector procedure :: add_matrix procedure,public :: next_row procedure,public :: close => close_csv_file procedure :: tokenize => tokenize_csv_line procedure :: read_line_from_file procedure :: get_column end type csv_file