网络书屋(Web Reading Room)

A blogging framework for hackers.

Fortran读取csv文件

本文只是简单对一个文件读取模块DFile_mod的一个运用,并读取逗号分隔的csv文件。

csvread

FileMod.f90:

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
Module DFile_Mod
  Implicit None
  !!**************************************
  !*  识别文件的空格和逗号作为分隔符
  !!*************************************** 
contains 

subroutine binaryStreamType
implicit none
integer :: File_Unit
integer :: File_UnitOutput
   !!**************************************
    !* DFile_Mod 变量定义  100%
    !!*************************************** 
    Character(len=512) :: cLine
    integer :: nRow, nCol
    character(len=20) :: name="yezhaoliang",addr="zhangzhou"
    INTEGER :: I
type :: data_head
integer(kind=2) :: column
end type 

type(data_head) :: FileHead
!real,allocatable :: g(:,:)
real,allocatable :: firstColumn(:)
real,allocatable :: secondColumn(:)
real,allocatable :: thirdColumn(:)

 Open( NewUnit=File_Unit , File = './data/myfile.csv' )
 Open( NewUnit=File_UnitOutput , File = './data/myfile12.csv' )

  nRow = GetFileN( File_Unit )
  write( * , * ) '文件共',nRow,'行!'
  read(File_Unit,*) FileHead
  write(*,*) 'm=',FileHead%column

  allocate(firstColumn(nRow-1))
  allocate(secondColumn(nRow-1))
  allocate(thirdColumn(nRow-1))

  !! 流方式读取
!  READ(File_Unit) g

250 format(3f10.4)
251 format(I4,I4,f10.4)
252 format (f10.4,A,f10.4,A,f10.4)
253 format (I4,A,I4,A,f10.4)
Do i = 1,nRow-1
    read(File_Unit,251) firstColumn(i),secondColumn(i),thirdColumn(i)
 end Do
  DO i = 1 ,nRow-1
   !write(*,'(f10.4,A,f10.4,A,f10.4)')  firstColumn(i),',',secondColumn(i),',',thirdColumn(i)
   write(File_UnitOutput,253)  firstColumn(i)*2,',--',secondColumn(i)*2,',--',thirdColumn(i)
  end do

deallocate(firstColumn)
deallocate(secondColumn)
deallocate(thirdColumn)
close(File_Unit)
close(File_UnitOutput)

  !Do i = 1, nRow
  !  Do j = 1, FileHead%column
  !
  !  end do
  !end do

end subroutine binaryStreamType


subroutine testType
implicit none
integer :: File_Unit
integer :: File_UnitOutput
   !!**************************************
    !* DFile_Mod 变量定义  100%
    !!*************************************** 
    Character(len=512) :: cLine
    integer :: nRow, nCol
    character(len=20) :: name="yezhaoliang",addr="zhangzhou"
    INTEGER :: I
type :: data_head
integer(kind=2) :: column
end type 

type(data_head) :: FileHead
!real,allocatable :: g(:,:)
real,allocatable :: firstColumn(:)
real,allocatable :: secondColumn(:)
real,allocatable :: thirdColumn(:)

 Open( NewUnit=File_Unit , File = './data/A001.csv' )
 Open( NewUnit=File_UnitOutput , File = './data/A0012.csv' )

  nRow = GetFileN( File_Unit )
  write( * , * ) '文件共',nRow,'行!'
  read(File_Unit,*) FileHead
  write(*,*) 'm=',FileHead%column

  allocate(firstColumn(nRow-1))
  allocate(secondColumn(nRow-1))
  allocate(thirdColumn(nRow-1))

  !! 流方式读取
!  READ(File_Unit) g

250 format(3f10.4)
251 format(I4,I4,f10.4)
252 format (f10.4,A,f10.4,A,f10.4)
253 format (I4,A,I4,A,f10.4)
Do i = 1,nRow-1
    read(File_Unit,251) firstColumn(i),secondColumn(i),thirdColumn(i)
 end Do
  DO i = 1 ,nRow-1
   !write(*,'(f10.4,A,f10.4,A,f10.4)')  firstColumn(i),',',secondColumn(i),',',thirdColumn(i)
   write(File_UnitOutput,253)  firstColumn(i)*2,',--',secondColumn(i)*2,',--',thirdColumn(i)
  end do

deallocate(firstColumn)
deallocate(secondColumn)
deallocate(thirdColumn)
close(File_Unit)
close(File_UnitOutput)

  !Do i = 1, nRow
  !  Do j = 1, FileHead%column
  !
  !  end do
  !end do

end subroutine testType


  Integer Function GetDataN( cStr )
    Character( Len = * ) , Intent( IN ) :: cStr
    Integer :: i
    Logical :: bIsSeparator , bIsQuote
    GetDataN = 0
    bIsSeparator = .TRUE.
    bIsQuote = .FALSE.
    Do i = 1 , Len_Trim( cStr )
      Select Case( cStr(i:i) )
      Case( '"' , "'" ) !// 如果遇到引号
        If ( .Not.bIsQuote ) GetDataN = GetDataN + 1  !//如果不在引号中,则增加一个数据
        bIsQuote = .Not.bIsQuote !// 引号结束或开始
        bIsSeparator = .FALSE.
      Case( " " , "," , char(9) ) !// 如果遇到分隔符
        If ( .Not.bIsQuote ) then  !// 分隔符如果不在引号中
          bIsSeparator = .TRUE.
        End If
      Case Default
        If ( bIsSeparator ) then
          GetDataN = GetDataN + 1
        End If
        bIsSeparator = .FALSE.
      End Select
    End Do
  End Function GetDataN

  Function f_numbervars(vars) result(numvars)
    character(len=*), intent(in) :: vars
    integer :: numvars
    character(len=len(vars)) :: tmpvars
    character(len=256) :: tmpvar
    tmpvars = trim(adjustl(vars))
    numvars = 0
    do while (len_trim(tmpvars) > 0)
      read(tmpvars, *) tmpvar
      numvars = numvars + 1
      tmpvars = tmpvars(index(tmpvars, trim(tmpvar))+len_trim(tmpvar):)
    end do
  End Function f_numbervars

  Integer Function GetFileN( iFileUnit )
  !// 此函数应在打开文件后立即调用。调用后读取位置返回文件起始位置
    Implicit None
    Integer , Intent( IN ) :: iFileUnit
    character( Len = 1 ) :: cDummy
    integer :: ierr
    GetFileN = 0
    Rewind( iFileUnit )
    Do
      Read( iFileUnit , * , ioStat = ierr ) cDummy
      If( ierr /= 0 ) Exit
      GetFileN = GetFileN + 1
    End Do
    Rewind( iFileUnit )
  End Function GetFileN

End Module DFile_Mod

主函数main.f90:

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
program Main

use DFile_Mod
Implicit none

   INTEGER :: I
   !!**************************************
    !* DFile_Mod 变量定义  100%
    !!*************************************** 
    !Character(len=512) :: cLine
    !integer :: nRow, nCol
    !character(len=20) :: name="yezhaoliang",addr="zhangzhou" 
    !**************************************
    !*  测试DFile_Mod   100%
    !*************************************** 

 !Open( 53 , File = './data/in.txt' )
  !Open( 53 , File = './data/myfile.csv' )
  !
  !nRow = GetFileN( 53 )
  !write( * , * ) '文件共',nRow,'行!'
  !Do i = 1 , nRow
  !  read( 53 , '(a512)' ) cLine
  !  nCol = GetDataN( cLine )
  !  !nCol = f_numbervars( cLine )
  !  write( * , * ) i,'行有',nCol,'个数据'
  !End Do
  call binaryStreamType

  Close( 53 )
  end program Main

配置文件in.txt:

1
2
3
4
5
1 2 3 , fsd
4 5 6 7 : asd
8 9 10 11 12
13 f sadf ! gad
14 15

载入visual studio基本上就能运行。