从fortran的.csv文件读取数据 [英] Read data from a .csv file in fortran

查看:900
本文介绍了从fortran的.csv文件读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个软件,给我一个输出一个 .csv 文件,我想借助于fortron代码阅读。 .csv 文件具有以下形式:

I am using a software that gives me as an output a .csv file which I want to read with the aid of a fortran code. The .csv file has the following form :

balance for 1. Unit: kg N/ha
___________________________________________________________________________________________________________________________________________________________________________
,N Pools,,,,,Influx N,,,,,Efflux N
Day,iniSON,iniSIN,endSON,endSIN,dSoilN,Deposit,Fertilizer,Manure,Litter,Sum-In...(**20 parameters**)
___________________________________________________________________________________________________________________________________________________________________________
 1,5973.55,  20.20,5973.51,  20.23,  -0.00,   0.00,   0.00,   0.00,   0.00,   0.00,   0.00,   0.00,   0.00,   0.00,   0.00,   0.00,   0.00,  -0.00,   0.00
.........

我有365行这样的值。

I have 365 lines with such values.

为了读取第一行,我使用了以下:

In order to read the first lines I used the following :

program od

implicit none
integer :: res
character(LEN=200) :: head1,head2,head3,head4,head5
open(10, file="Balance_N_1.csv",access='sequential',form="formatted",iostat=res)
open(9,file="out.txt")
read(10,fmt='(A)', iostat=res) head1,head2,head3,head4,head5
write(9,*) head1,head2,head3,head4,head5       

end program od

如何读取随后的数据并将它们放在一个矩阵中,以便我可以用一些值执行计算?

How is it possible to read the data that follow and to put them in a matrix so that I can perform calculations with some of the values?

推荐答案

如果我读取您的问题,您有一个包含365行数据的文件,每行有1个整数)和20个实数,并且每行上的值以逗号分隔。你可以这样声明一个数据数组:

If I read your question right you have a file with 365 lines of data, each line has 1 integer (the day number) and 20 real numbers and the values on each line are comma-separated. You could declare a data array like this;

real, dimension(365,20) :: data_array

和一个整数变量,例如

integer :: line_no

然后,一旦你阅读或跳过,文件顶部的文本,可以像这样读取数组:

then, once you've read, or skipped over, the lines of text at the top of your file, you can read the array like this:

do ix = 1,365
    read(10,*) line_no, data_array(ix,:)
end do

* 对于您正在使用列表引导的输入的读取语句中的格式,这是快速和容易,但有些限制。但是,如果你的数据文件是干净和一致的,这应该是够好了。

By using * for the format in your read statement you are using list-directed input which is quick and easy but somewhat limited. However, if your data file is clean and consistent this should be good enough.

如果列表导向的输入不工作,你必须使用编辑描述符,

If list-directed input doesn't work you'll have to use edit descriptors, something like this (untested)

read(10,'(i4,20f9.4)') line_no, data_array(ix,:)

这篇关于从fortran的.csv文件读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆