fortran抽取csv文件中某几列,写成新文件(新手求教)
用fortran读取csv文件,文件如下(部分截图):一共792305行,113列,现要将其中名为alloc_key,whea,whea_h,whea_l, whea_i,whea_s等6列的全部数据抽取出,写入另一个文件里我写了以下程序:
program wheat_area
implicit none
integer m,i
integer,allocatable::alloc_key(:)
real,allocatable::whea(:), whea_h(:),whea_l(:)
real,allocatable::whea_i(:),whea_s(:)
open(10,file="/home/iga_qtong/fortran_xcao/gae_c/spam_p.csv")
open(11,file="/home/iga_qtong/fortran_xcao/gae_c/whout.csv")
m=792305
allocate(alloc_key(m),whea(m),whea_h(m))
allocate(whea_l(m),whea_i(m),whea_s(m))
do i=1,1
read(10,*)
end do
do i=1,m
read(10,*)
write(11,*) (alloc_key(i),whea(i),whea_h(i),whea_l(i),&
whea_i(i),whea_s(i))
end do
close(10)
close(11)
end program wheat_area
完全新手,有很多问题要请教
请问:
1、是否可以只读要抽取数据的几列的表头
2、write续行符使用有问题
3、请高手帮助指出程序的问题
write的问题,去掉最外层括号就好。program test
implicit none
integer,parameter::nCol = 113
integer i, j
character(80) str(nCol)
character(*),parameter:: strHead(6)=['alloc_key','whea','whea_h','whea_l', 'whea_i','whea_s']
logical L(nCol)
open(11,file='2.csv')
open(10,file='1.csv')
!读取表头
read(10,*) str
!判断需要读取的列
L = .false.
do i = 1, nCol
str(i) = adjustl( str(i) )
if( any(trim(str(i))==strHead) ) then
L(i) = .true.
write(11,"(a,',')",advance='no') trim( str(i) ) !输出新表头
end if
end do
write(11,*)
do
read(10,*,iostat=j) str
if(j/=0) exit
do i =1, nCol
if(.NOT.L(i)) cycle
write(11,'(a,",")',advance='no') trim( str(i) )
end do
write(11,*)
end do
end program test
li913 发表于 2017-4-8 18:23
write的问题,去掉最外层括号就好。program test
implicit none
integer,pa ...
do i =1, nCol
if(.NOT.L(i)) cycle
write(11,'(a,",")',advance='no') trim( str(i) )
这个cycle的用法不太理解:在cycle命令后面的所有程序代码,直接跳回循环的开头来进行下一次循环。
如果用其它循环,比如do可以如何写呢? 这本来就是 do 循环呀,加了 cycle 它还是do循环。
cycle 的意思是,放弃本次循环,直接进入下一次循环。 fcode 发表于 2017-4-9 17:32
这本来就是 do 循环呀,加了 cycle 它还是do循环。
cycle 的意思是,放弃本次循环,直接进入下一次循环。 ...
但是这个程序在Linux下编译的时候还是有很多错误,
character(len=*),parameter::strHead(6)=('alloce_key,'whea','whea_h','whea_l','whea_i','whea_s')
logical (kind=4) L(nCol)
这个地方还是有错误,请问怎么改? character(*),parameter:: strHead(6)=['alloc_key','whea','whea_h','whea_l', 'whea_i','whea_s']
改为
character(9),parameter:: strHead(6)=['alloc_key','whea ','whea_h ','whea_l ', 'whea_i ','whea_s ']
fcode 发表于 2017-4-10 08:10
character(*),parameter:: strHead(6)=['alloc_key','whea','whea_h','whea_l', 'whea_i','whea_s']
改为
你好,我按原博文写的这个程序,
program wheat_area
implicit none
integer k,i
integer,parameter::nCol=113
!Define a constant nCol equal to 113
character(80) str(nCol)
character(9),parameter::strHead(6)=['alloc_key,'whea ','whea_h ','whea_l ','whea_i ','whea_s ']
logical (kind=4) L(nCol)
open(10,file="/home/iga_qtong/fortran_xcao/gae_c/spam_p.csv")
open(11,file="/home/iga_qtong/fortran_xcao/gae_c/whout.csv")
read(10,*) str!read the head of table
L= .false. !determin the column to read
do i=1,nCol
str(i)=AdjustL(str(i))
if(any(Trim(str(i))==strHead))then
!if the string equal to the strHead
L(i)= .true.
write(11,"(a,',')",advance='no') Trim(str(i))
end if
end do
编译时还是出现了以下错误:请问如何改正?还有在Linux下编译fortran,定义变量时过长不能编译,续行符我始终没有查明白,请赐教,谢谢
wheat_area.f(8): error #5144: Invalid character_kind_parameter. No underscore
character(9),parameter::strHead(6)=['alloc_key,'whea ','whea_h ','whea_l ','whea_i ','whea_s ']
------------------------------------------------------------------^
wheat_area.f(8): error #5082: Syntax error, found IDENTIFIER 'WHEA' when expecting one of: ( * ) :: , <END-OF-STATEMENT> ; . (/ + - : ] /) ' ** / // > .LT. ...
character(9),parameter::strHead(6)=['alloc_key,'whea ','whea_h ','whea_l ','whea_i ','whea_s ']
------------------------------------------------------^
wheat_area.f(17): error #6404: This name does not have a type, and must have an explicit type.
if(any(Trim(str(i))==strHead))then
------------------------------^
wheat_area.f(17): error #6303: The assignment operation or the binary expression operation is invalid for the data types of the two operands.
if(any(Trim(str(i))==strHead))then
------------------------------^
wheat_area.f(17): error #6361: An array-valued argument is required in this context.
if(any(Trim(str(i))==strHead))then
----------------------------^
compilation aborted for wheat_area.f (code 1) fcode 发表于 2017-4-10 08:10
character(*),parameter:: strHead(6)=['alloc_key','whea','whea_h','whea_l', 'whea_i','whea_s']
改为
对不起,之前粘贴的程序不完整:
program wheat_area
implicit none
integer k,i
integer,parameter::nCol=113
!Define a constant nCol equal to 113
character(80) str(nCol)
character(9),parameter::strHead(6)=['alloc_key,'whea ','whea_h ','whea_l ','whea_i ','whea_s ']
logical (kind=4) L(nCol)
open(10,file="/home/iga_qtong/fortran_xcao/gae_c/spam_p.csv")
open(11,file="/home/iga_qtong/fortran_xcao/gae_c/whout.csv")
read(10,*) str!read the head of table
L= .false. !determin the column to read
do i=1,nCol
str(i)=AdjustL(str(i))
if(any(Trim(str(i))==strHead))then
!if the string equal to the strHead
L(i)= .true.
write(11,"(a,',')",advance='no') Trim(str(i))
end if
end do
write(11,*)
do
read(10,*,iostat=k) str
if(k/=0) exit
do i=1,nCol
if(.NOT.L(i)) cycle
write(11,'(a,",")',advance='no') Trim(str(i))
end do
write(11,*)
end do
end program wheat_area 少了一个引号,请直接复制粘贴我的代码。 fcode 发表于 2017-4-10 17:20
少了一个引号,请直接复制粘贴我的代码。
我复制了您的代码之后 ,还是出现以下错误,说这种定义不合法,linux下(实在太不好意思,一直麻烦您):
wheat_area.f(8): error #5120: Unterminated character constant
character(9),parameter:: strHead(6)=['alloc_key','whea','whea_h ','whea_l ', 'whea_i ','whea_s ']
--------------------------------------------------------------^
wheat_area.f(8): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: , (/ : ]
character(9),parameter:: strHead(6)=['alloc_key','whea','whea_h ','whea_l ', 'whea_i ','whea_s ']
---------------------------------------------------------------------------------------------------------------^
wheat_area.f(8): error #6366: The shapes of the array expressions do not conform.
character(9),parameter:: strHead(6)=['alloc_key','whea','whea_h ','whea_l ', 'whea_i ','whea_s ']