0% found this document useful (0 votes)
22 views

csv.f90 1/2

This Fortran program parses a CSV file with two columns, a header line and data lines. It defines data types for the header and data lines. Subroutines are used to parse the header and data lines, extracting the column values. The program then reads the CSV file, calls the subroutines to parse each line, and writes the parsed data to output.

Uploaded by

cosmimthat
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

csv.f90 1/2

This Fortran program parses a CSV file with two columns, a header line and data lines. It defines data types for the header and data lines. Subroutines are used to parse the header and data lines, extracting the column values. The program then reads the CSV file, calls the subroutines to parse each line, and writes the parsed data to output.

Uploaded by

cosmimthat
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

csv.

f90

1/2
03-03-2012 22:28:01

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:

module csv_mod
type csv_header_line
character(10) :: c01 ! column 01
character(10) :: c02 ! column 02
end type csv_header_line
type csv_data_line
character(10):: c01 ! column 01
:: c02 ! column 02
integer
end type csv_data_line
contains
subroutine parse_header(separator, line, header)
character(*), intent(in) :: separator, line
type(csv_header_line), intent(out) :: header
integer :: separator_pos
!separator position
separator_pos = index(line, separator)
header%c01 = adjustl(trim(line( :separator_pos-1)))
header%c02 = adjustl(trim(line(separator_pos+1: )))
end subroutine parse_header
!**********************************************************************
subroutine parse_data(separator, line, dta, nr)
character(*), intent(in) :: separator, line
integer, intent(in) :: nr
type(csv_data_line), dimension(:), intent(inout) :: dta
integer :: separator_pos
!separator position
separator_pos = index(line, separator)
dta(nr)%c01 = adjustl(trim(line(:separator_pos-1)))
! convert substring into integer number
read (line(separator_pos+1:),'(I3)') dta(nr)%c02
end subroutine parse_data
end module csv_mod

!-------------------------------------------------------------------------------------------------59: !-------------------------------------------------------------------------------------------------60:
61: program csv_read
62:
use csv_mod
63:
implicit none
64:
65:
integer, parameter :: max_csv_lines = 100 ! max number of lines in CSV
66:
= 2
integer, parameter :: csv_columns
! number of columns in CSV
67:
integer stat, line_nr, nr, j, computed_number
68:
character :: separator = ';'
69:
character(80) :: line
70:
type(csv_header_line) :: csv_header

1/2

csv.f90

2/2
03-03-2012 22:28:01

71:
type(csv_data_line), dimension(max_csv_lines) :: csv_data
72:
73:
! open file
74:
75:
open (1, file='csv_file.csv', status='old', iostat=stat)
76:
77:
if (stat .ne. 0) then
78:
write(*,*) 'File cannot be opened !'
79:
go to 99
80:
end if
81:
82:
write(*,*) 'Reading CSV-file...'
83:
! process file
84:
85:
line_nr = 0
86:
87:
do while (.true.)
88:
read(1, '(A)', end=99) line
89:
90:
line_nr = line_nr + 1
91:
92:
if (line_nr .eq. 1) then
93:
94:
call parse_header(separator, adjustl(trim(line)), csv_header)
95:
96:
else
97:
98:
! if line_nr > 1 then parse data line
99:
nr = line_nr - 1
100:
101:
call parse_data(separator, adjustl(trim(line)), csv_data, nr)
102:
103:
end if
104:
105:
end do
106:
107:
! close file
108:
109:
99 continue
110:
111:
close(1)
112:
113:
write(*,*) 'Done.'
114:
115:
write(*,*) 'Number of all lines found in CSV = ', line_nr
116:
write(*,*) 'Number of data lines found in CSV = ', nr
117:
118:
! write the data
119:
write(*, '(A)') '****************************************'
120:
write(*,'(A10, A10, A10)') csv_header%c01, &
121:
adjustr(csv_header%c02), &
122:
'3*NUMCOL'
123:
write(*, '(A)') '****************************************'
124:
do j = 1, nr
125:
computed_number = 3 * csv_data(j)%c02
126:
write (*,'(A10, I10, I10)') csv_data(j)%c01, &
127:
csv_data(j)%c02, &
128:
computed_number
129:
130:
end do
131:
write(*, '(A)') '****************************************'
132: end program csv_read

2/2

You might also like