Re: How to read a csv file when the date comes with /?



On 29 jun, 07:31, Thomas Koenig <tkoe...@xxxxxxxxxxxxx> wrote:
On 2008-06-29, Ed <emammen...@xxxxxxxxx> wrote:

Hello

I know that there is a bunch of threads on the subject but I don't
seem to get the rirght way of coding a specific code for my problem.

Here is the cvs dat format (too many to change format)

5,01/02/2008 15:00:00,01/02/2008 15:00:00, 29.48,     1298,   329..6
   write (unit=*,FMT=*) line

That doesn't work for the presence of slashes (as you noted yourself).

Here's some sample code that parses a single line.  Once you have the
fields, you can then parse the individual data items with internal
reads.  The idea is to scan the line for the delimiters, save the
delimiter positions in an array, and then use these to determine which
parts of the input line to look at.

A vector-valued equivalent of the INDEX intrinsic would come in handy,
or an equivalent of Perl's 'split' function :-)

program main
  implicit none
  integer, parameter :: ndat = 6
  character(len=80) line
  integer, dimension(0:ndat) :: spos
  integer :: ipos
  integer :: i

  read(*,'(A)') line
  ipos = 0
  spos(ipos) = 0
  do i=1,len(line)
     if (line(i:i) == ',') then
        ipos = ipos + 1
        if (ipos > ndat -1 ) stop 'too many data items'
        spos(ipos) = i
     end if
  end do
  if (ipos + 1 /= ndat) stop 'not enough data items'
  spos(ndat) = len(line)+1
  do i=1,ndat
     print '(A)',line(spos(i-1)+1:spos(i)-1)
  end do
end program main

Mnay many thanks. It worked. The only thing that is missing is how to
attribute the contents of line(spos(i-1)+1:spos(i)-1) (for i=2 and
3,strings with the date and hour) to dh and dhb.

Any ideas?

Ed
.



Relevant Pages