Fortran Coder

标题: fortran抽取csv文件中某几列,写成新文件(新手求教) [打印本页]

作者: 桂圆莲子汤    时间: 2017-4-8 16:55
标题: fortran抽取csv文件中某几列,写成新文件(新手求教)
用fortran读取csv文件,文件如下(部分截图):一共792305行,113列,现要将其中名为alloc_key,whea,whea_h,whea_l, whea_i,whea_s等6列的全部数据抽取出,写入另一个文件里

我写了以下程序:
[Fortran] 纯文本查看 复制代码
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、请高手帮助指出程序的问题

作者: li913    时间: 2017-4-8 18:23
write的问题,去掉最外层括号就好。
[Fortran] 纯文本查看 复制代码
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




作者: 桂圆莲子汤    时间: 2017-4-9 15:25
li913 发表于 2017-4-8 18:23
write的问题,去掉最外层括号就好。[mw_shl_code=fortran,true]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可以如何写呢?
作者: fcode    时间: 2017-4-9 17:32
这本来就是 do 循环呀,加了 cycle 它还是do循环。

cycle 的意思是,放弃本次循环,直接进入下一次循环。
作者: 桂圆莲子汤    时间: 2017-4-9 23:52
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)
这个地方还是有错误,请问怎么改?
作者: fcode    时间: 2017-4-10 08:10
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   ']

作者: 桂圆莲子汤    时间: 2017-4-10 10:54
fcode 发表于 2017-4-10 08:10
character(*),parameter:: strHead(6)=['alloc_key','whea','whea_h','whea_l', 'whea_i','whea_s']

改为

你好,我按原博文写的这个程序,
[Fortran] 纯文本查看 复制代码
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.   [STRHEAD]
         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.   [STRHEAD]
         if(any(Trim(str(i))==strHead))then
------------------------------^
wheat_area.f(17): error #6361: An array-valued argument is required in this context.   [ANY]
         if(any(Trim(str(i))==strHead))then
----------------------------^
compilation aborted for wheat_area.f (code 1)
作者: 桂圆莲子汤    时间: 2017-4-10 10:55
fcode 发表于 2017-4-10 08:10
character(*),parameter:: strHead(6)=['alloc_key','whea','whea_h','whea_l', 'whea_i','whea_s']

改为

对不起,之前粘贴的程序不完整:
[Fortran] 纯文本查看 复制代码
     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
少了一个引号,请直接复制粘贴我的代码。
作者: 桂圆莲子汤    时间: 2017-4-10 21:11
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.   [STRHEAD]
      character(9),parameter:: strHead(6)=['alloc_key','whea','whea_h   ','whea_l   ', 'whea_i   ','whea_s   ']
作者: fcode    时间: 2017-4-10 21:22
没想到你连复制粘贴都不会
作者: 桂圆莲子汤    时间: 2017-4-10 21:51
fcode 发表于 2017-4-10 21:22
没想到你连复制粘贴都不会

对不起,之前把空格去掉了,我自己没注意,不好意思,可是这次我是真的复制过去的,还是一样的错误,但是如果我把它定义2个变量,是可以编译的,只要编译三个以上,长度超出之后,还是这个错误,所以请问是否是应该使用续行符的问题:
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.   [STRHEAD]
      character(9),parameter:: strHead(6)=['alloc_key','whea     ','whea_h   ','whea_l   ', 'whea_i   ','whea_s   ']
-------------------------------^
compilation aborted for wheat_area.f (code 1)


作者: pasuka    时间: 2017-4-10 22:11
桂圆莲子汤 发表于 2017-4-10 21:51
对不起,之前把空格去掉了,我自己没注意,不好意思,可是这次我是真的复制过去的,还是一样的错误,但是 ...

*.f格式最大列数不超过80,把文件后缀名改成*.f90

活学活用,急用先学并不代表本站的视频教程和彭国伦的书无关紧要啊
作者: fcode    时间: 2017-4-11 07:53
太长了可以续行
作者: 桂圆莲子汤    时间: 2017-4-11 09:18
pasuka 发表于 2017-4-10 22:11
*.f格式最大列数不超过80,把文件后缀名改成*.f90

活学活用,急用先学并不代表本站的视频教程和彭国伦的 ...

谢谢,非常感谢
作者: 山水    时间: 2017-10-7 11:06
[Fortran] 纯文本查看 复制代码
program test
implicit none  
integer,parameter::nCol = 36
  integer i, j
  character(80) str(nCol)
  
character(32),parameter::  strHead(1)=['Province']
!strHead(23)=['Province','City','Stationname','Longitude','Latitude','Publishtime', 'AQI','SO2','NO2','CO','O3','PM10', &
!'PM2_5','SO2_24H_AVG','NO2_24H_AVG','CO_24H_AVG','O3_24H_AVG','PM10_24H_AVG','PM2_5_24H_AVG','O3_8H','O3_8H_24H','PrimaryPollutant','Quality']

  logical L(nCol)

  open(11,file='F:\GraduateStudent\Data-Data\sichuanpollution.csv')
  open(10,file='F:\GraduateStudent\Data-Data\sichuanpollution1.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

我复制粘贴后按照自己需要的修改了一下,不管是定义1个变量还是3个甚至更多变量,都会出现这样一个错误:error FOR3852: syntax error detected between = and ['Province'],想问一下是什么原因呢

作者: fcode    时间: 2017-10-9 09:54
山水 发表于 2017-10-7 11:06
[mw_shl_code=fortran,true]program test
implicit none  
integer,parameter::nCol = 36

请放弃 PowerStation 这种古老的编译器。
作者: 逗逗逗儿啵    时间: 2017-12-17 10:15
fcode 发表于 2017-4-10 17:20
少了一个引号,请直接复制粘贴我的代码。

按照上面的代码复制了,但是因为原文件有些数据是空缺的,生成的文件空缺的地方自动补充了,请问您有什么解决办法嘛~
[Fortran] 纯文本查看 复制代码
program test
  implicit none  
integer,parameter::nCol = 949
  integer i, j
  character(10000) str(nCol)
  character(*),parameter:: strHead(9)=['date','hour','type','1142A','1143A','1144A','1145A', '1146A','1147A']
  logical L(nCol)
  open(10,file='E:\shuju\zhandian\20140513-20141231\china_sites_20141201.csv')
  open(11,file='E:\shuju\china_sites_20141201.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



捕获.PNG (20.74 KB, 下载次数: 383)

捕获.PNG

捕获1.PNG (18.17 KB, 下载次数: 434)

捕获1.PNG

作者: Rookie    时间: 2022-4-14 16:50
li913 发表于 2017-4-8 18:23
write的问题,去掉最外层括号就好。[mw_shl_code=fortran,true]program test
  implicit none  
integer,pa ...

请问第19和28行的 write语句括号中的  format  部分,所用的单引号、双引号,刚好反过来了。这是什么原因呢?
作者: 青衣巷    时间: 2022-4-14 17:10
Rookie 发表于 2022-4-14 16:50
请问第19和28行的 write语句括号中的  format  部分,所用的单引号、双引号,刚好反过来了。这是什么原因 ...

没关系的,Fortran不区分单双引号。
只要间隔开就行了。
比如你用 " 开头,里面就要用 ',例如 "I ' m fcode"
而用 ' 开头,里面就要用 " ,例如 ' I "m fcode '
作者: Rookie    时间: 2022-4-18 09:28
青衣巷 发表于 2022-4-14 17:10
没关系的,Fortran不区分单双引号。
只要间隔开就行了。
比如你用 " 开头,里面就要用 ',例如 "I ' m fc ...

懂了,谢谢指教




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2