Fortran Coder

查看: 4498|回复: 0

CSV文件读写模块

[复制链接]

127

帖子

35

主题

1

精华

大师

F 币
1150 元
贡献
592 点
发表于 2019-5-27 19:34:14 | 显示全部楼层 |阅读模式
本帖最后由 weixing1531 于 2019-5-27 19:36 编辑

CSV文件能被excel直接打开,处理数据时作用很大

以前读写CSV文件时都是人工的加上逗号分隔符,极不方便

GitHub上面有一个CSV文件读写模块,将CSV文件读写操作进行了封装
下载网址:https://github.com/jacobwilliams/fortran-csv-module

示例:
[Fortran] 纯文本查看 复制代码
!*****************************************************************************************
!> author: Jacob Williams
!  license: BSD
!
!  Test of reading and writing CSV files.

    program csv_test

    use csv_module
    use iso_fortran_env, only: wp => real64

    implicit none

    type(csv_file) :: f
    type(csv_file) :: f2
    integer :: i !! counter
    character(len=30),dimension(:),allocatable :: header  !! the header
    character(len=30),dimension(:,:),allocatable :: csv_data  !! the data from the file as strings
    real(wp),dimension(:),allocatable :: x  !! for getting a real vector from a csv file
    logical :: status_ok  !! error flag
    integer,dimension(:),allocatable :: itypes  !! array of variable types in the file
    integer :: ifile !! file counter
    character(len=30),dimension(:),allocatable :: names

!    character(len=*),dimension(2),parameter :: files_to_test = ['../files/test.csv          ',&
!                                                                '../files/test_2_columns.csv']
    character(len=*),dimension(2),parameter :: files_to_test = ['test.csv          ',&
                                                                'test_2_columns.csv']

    do ifile = 1, size(files_to_test)

        ! read the file:
        if (ifile==1) then
            call f%read(trim(files_to_test(ifile)),&
                        header_row=1,status_ok=status_ok)
        else
            ! also skip a row
            call f%read(trim(files_to_test(ifile)),&
                        header_row=1,skip_rows=[2],status_ok=status_ok)
        end if

        write(*,*) ''
        write(*,*) 'File: '//trim(files_to_test(ifile))
        ! print the header and type info:
        call f%get_header(header,status_ok) !获取列的名称
        call f%variable_types(itypes,status_ok) !获取列的数据类型
        !1代表字符串 2代表浮点数 3代表整数 4代表逻辑变量
        write(*,*) ''
        write(*,'(*(A30,1X,A4))') 'Header', 'Type'
        do i=1,size(header)
            write(*,'(*(A30,1X,I4))') header(i), itypes(i)
        end do

        write(*,*) ''
        write(*,*) 'print all the rows:'

        call f%get(csv_data,status_ok)
        do i=1,size(csv_data,1) !第一维为行数 第二维为列数
            write(*,'(*(A30,1X))') csv_data(i,:)
        end do

        write(*,*) ''
        write(*,*) 'get some vectors:'
        if (ifile==1) then
            write(*,*) ''
            write(*,*) 'age:'
            call f%get(3,x,status_ok) !获取第3列数据
            write(*,'(F6.3,1x)',advance='NO') x
            write(*,*) ''
        else
            write(*,*) ''
            write(*,*) 'name:'
            call f%get(2,names,status_ok) !获取第2列数据
            write(*,'(A10,1x)',advance='NO') names
            write(*,*) ''
        end if

    end do

    ! now test creating a CSV:
    call f2%initialize(enclose_strings_in_quotes=.false.,verbose=.true.)
    call f2%open('test2.csv',n_cols=4,status_ok=status_ok) !n_cols数据列数
    if (status_ok) then
        call f2%add(['x','y','z','t'])    ! add header as vector
        call f2%next_row() ! 换行
        call f2%add(1.0_wp)  ! add as scalars
        call f2%add(2.0_wp)
        call f2%add(3.0_wp)
        call f2%add(.true.)
        call f2%next_row() ! 换行
        call f2%add([4.0_wp,5.0_wp,6.0_wp],real_fmt='(F5.3)') ! add as vectors
        call f2%add(.false.)
        call f2%next_row() ! 换行
    end if
    call f2%close(status_ok)

    end program csv_test
!*****************************************************************************************

源代码如下:
program.zip (518.48 KB, 下载次数: 9)
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

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

GMT+8, 2024-4-19 05:39

Powered by Tencent X3.4

© 2013-2024 Tencent

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