Fortran Coder

查看: 41524|回复: 20
打印 上一主题 下一主题

[文件读写] fortran抽取csv文件中某几列,写成新文件(新手求教)

[复制链接]

25

帖子

8

主题

0

精华

熟手

F 币
164 元
贡献
104 点
跳转到指定楼层
楼主
发表于 2017-4-8 16:55:31 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
用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、请高手帮助指出程序的问题
分享到:  微信微信
收藏收藏2 点赞点赞 点踩点踩

796

帖子

2

主题

0

精华

大宗师

F 币
3787 元
贡献
2266 点
沙发
发表于 2017-4-8 18:23:48 | 只看该作者
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



25

帖子

8

主题

0

精华

熟手

F 币
164 元
贡献
104 点
板凳
 楼主| 发表于 2017-4-9 15:25:15 | 只看该作者
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可以如何写呢?

1958

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1339 元
贡献
565 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

地板
发表于 2017-4-9 17:32:06 | 只看该作者
这本来就是 do 循环呀,加了 cycle 它还是do循环。

cycle 的意思是,放弃本次循环,直接进入下一次循环。

25

帖子

8

主题

0

精华

熟手

F 币
164 元
贡献
104 点
5#
 楼主| 发表于 2017-4-9 23:52:53 | 只看该作者
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)
这个地方还是有错误,请问怎么改?

1958

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1339 元
贡献
565 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

6#
发表于 2017-4-10 08:10:59 | 只看该作者
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   ']

25

帖子

8

主题

0

精华

熟手

F 币
164 元
贡献
104 点
7#
 楼主| 发表于 2017-4-10 10:54:32 | 只看该作者
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)

25

帖子

8

主题

0

精华

熟手

F 币
164 元
贡献
104 点
8#
 楼主| 发表于 2017-4-10 10:55:41 | 只看该作者
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

1958

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1339 元
贡献
565 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

9#
发表于 2017-4-10 17:20:11 | 只看该作者
少了一个引号,请直接复制粘贴我的代码。

25

帖子

8

主题

0

精华

熟手

F 币
164 元
贡献
104 点
10#
 楼主| 发表于 2017-4-10 21:11:32 | 只看该作者
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   ']
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-4-20 13:48

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表