Fortran 77: read second number on a line in a comma delimited text file 
Author Message
 Fortran 77: read second number on a line in a comma delimited text file

Hi Folks,

Is there a way to use format directed input in fortran 77 to read the
second number of each line in a comma delimited text file (.csv file)

file format:

1,6,3
7,5,7
1,3,7

I would like to read the numbers 6,5,3 into the vector x(1:3).

I can do it using a dummy variable
         do 100 i=1,3
              read(infile,*) dummy,x(i)
 100   continue

 but wondered if there is a more elegant way using the format
statement?

Many thanks

Richard



Fri, 29 Jul 2011 20:31:09 GMT  
 Fortran 77: read second number on a line in a comma delimited text file

Quote:
> Hi Folks,

> Is there a way to use format directed input in fortran 77 to read the
> second number of each line in a comma delimited text file (.csv file)

> file format:

> 1,6,3
> 7,5,7
> 1,3,7

> I would like to read the numbers 6,5,3 into the vector x(1:3).

> I can do it using a dummy variable
> ? ? ? ? ?do 100 i=1,3
> ? ? ? ? ? ? ? read(infile,*) dummy,x(i)
> ?100 ? continue

> ?but wondered if there is a more elegant way using the format
> statement?

There is no simpler way I know of, even in Fortran 95 or 2003.


Fri, 29 Jul 2011 22:02:38 GMT  
 Fortran 77: read second number on a line in a comma delimited text file
...
Quote:
> Is there a way to use format directed input in fortran 77 to read the
> second number of each line in a comma delimited text file (.csv file)

> file format:

> 1,6,3
> 7,5,7
> 1,3,7

> I would like to read the numbers 6,5,3 into the vector x(1:3).

> I can do it using a dummy variable
>          do 100 i=1,3
>               read(infile,*) dummy,x(i)
>  100   continue

>  but wondered if there is a more elegant way using the format
> statement?

...

It's hard to say it's "more elegant" owing to the limitations it
imposes, but certainly for the data shown one could use

    read(infile,'(2X,I1)') x(i)

The obvious problem is that for anything but precisely the one-column
width of the integers between 0-9 the read fails whereas the free format
handles it automagically.

The elegant way would be to read the line as a character and parse it,
but that elegance comes at the price of more complex coding.

--



Fri, 29 Jul 2011 23:32:27 GMT  
 Fortran 77: read second number on a line in a comma delimited text file
...
Quote:
> Is there a way to use format directed input in fortran 77 to read the
> second number of each line in a comma delimited text file (.csv file)

...

A more general musing after the previous response of a specific FORMAT
solution that has many limitations...

I suppose the idea of an "elegant" solution would be a FORMAT positional
specifier that is context-sensitive (for lack of better phraseology that
comes to mind at the moment) that would allow for skipping a
variable-width field.

Matlab (ML) has such a feature in some of its text input functions.  ML
uses a (vectorized) form of the C printf() format descriptors.  In the
function such as textread() which has quite a few options for handling
variously formatted text files, using %* instead of % in a conversion
causes textread() to skip the matching characters in the input and no
output is created for this conversion.  It also has a selectable field
delimiter character, so a csv file can be parsed by column in a single call.

Of course, all of this is implemented in a fairly complex routine that
does all the parsing of the input file so underneath the surface the
duck is paddling furiously although it looks quite serene from shore.

--



Sat, 30 Jul 2011 01:41:31 GMT  
 Fortran 77: read second number on a line in a comma delimited text file

Quote:

> ...> Is there a way to use format directed input in fortran 77 to read the
> > second number of each line in a comma delimited text file (.csv file)

> ...

> A more general musing after the previous response of a specific FORMAT
> solution that has many limitations...

> I suppose the idea of an "elegant" solution would be a FORMAT positional
> specifier that is context-sensitive (for lack of better phraseology that
> comes to mind at the moment) that would allow for skipping a
> variable-width field.

> Matlab (ML) has such a feature in some of its text input functions. ?ML
> uses a (vectorized) form of the C printf() format descriptors. ?In the
> function such as textread() which has quite a few options for handling
> variously formatted text files, using %* instead of % in a conversion
> causes textread() to skip the matching characters in the input and no
> output is created for this conversion. ?It also has a selectable field
> delimiter character, so a csv file can be parsed by column in a single call.

> Of course, all of this is implemented in a fairly complex routine that
> does all the parsing of the input file so underneath the surface the
> duck is paddling furiously although it looks quite serene from shore.

> --

There once was a proposal for options csv file reading in F2k8...I
think maybe it went away.


Sat, 30 Jul 2011 04:28:45 GMT  
 Fortran 77: read second number on a line in a comma delimited text file

Quote:

> There once was a proposal for options csv file reading in F2k8...I
> think maybe it went away.

For 2 reasons (in my opinion).

1. (This one might not be agreed on by all, but it was a point that at
least I argued). As proposed, I didn't think it it fit at all "cleanly"
with the rest of the language's I/O scheme. I forget all the details. I
think one that struck me was the oddity of essentially specifying format
details in the OPEN statement, which pretty much forced it to be a
whole-file property instead of something that could be used for part of
a file such as an individual line.

2. (I think this one is more widely agreed, and seems pretty relevant to
this thread anyway). The existing I/O already comes pretty close anyway.
I believe the conclusion was that adding a few extra fairly small
features to the existing I/O would make it do the job fine without
introducing a whole new language concept of a separate kind of
formatting. I don't off-hand recall what the features were, but I do
recall that they struck me as fairly minor features that should not be
difficult to add or to understand. Vague recollection is that one was an
enhancement of the G edit descriptor; I might misrecall.

But in any case, I don't see how you are going to get much more
"elegant" an answer to the OP's question than the one of reading a dummy
variable for the first field. There isn't any option like "read the
second comma-delimitted field". Sure, you can do the thing of reading a
line into a character buffer and parsing that buffer, but I don't see
how that is more "elegant". It sure takes a lot more user-written code.
It can well be more flexible/capable, but I wouldn't call it
particularly more elegant, which is what the OP asked for.

To me, elegance often correlates with parsimony. It isn't a perfect
correlation, but I see at least some. The relative parsimony of the
dummy variable approach is hard to beat by a lot in that regard. If
there were a "read the second field" edit descriptor, that would be more
parsimonious by eliminating the inelegance of the otherwise unused dummy
variable... but there isn't such a feature.

--
Richard Maine                    | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle           |  -- Mark Twain



Sat, 30 Jul 2011 05:41:58 GMT  
 Fortran 77: read second number on a line in a comma delimited text file
(snip)

Quote:
> But in any case, I don't see how you are going to get much more
> "elegant" an answer to the OP's question than the one of reading a dummy
> variable for the first field. There isn't any option like "read the
> second comma-delimitted field". Sure, you can do the thing of reading a
> line into a character buffer and parsing that buffer, but I don't see
> how that is more "elegant". It sure takes a lot more user-written code.
> It can well be more flexible/capable, but I wouldn't call it
> particularly more elegant, which is what the OP asked for.

The C formatted input routines (the scanf family) have the option
on the format descriptors not to store the value.  (the * option)
That makes it very easy to ignore what you might otherwise read.

scanf("%*d%d",&i);

will read two numbers in white space
delimited text and store only the second one.

It isn't quite as easy to read comma or tab delimited data, though.

If it is comma delimited without white space then

scanf("%*d,%d,",&i);

will do it, but it will stop if there isn't a comma.

scanf("%*d ,%d ,",&i);

should read comma delimited text with optional white
space around the comma.

I make no comment on the elegance of this, though.

-- glen



Sat, 30 Jul 2011 06:13:13 GMT  
 Fortran 77: read second number on a line in a comma delimited text file


Quote:
> To me, elegance often correlates with parsimony. It isn't a perfect
> correlation, but I see at least some. The relative parsimony of the
> dummy variable approach is hard to beat by a lot in that regard. If
> there were a "read the second field" edit descriptor, that would be more
> parsimonious by eliminating the inelegance of the otherwise unused dummy
> variable... but there isn't such a feature.

The other disadvantage of the dummy variable approach is if the ignored
field in the input file has the wrong data type.  For example, if you
try to read an integer IDUMMY, but the field contains a real variable
then there will be an I/O error.  Or if you try to read a real variable
DUMMY but the ignored field contains a character string.

As far as using the G format to do this, I think that would be great.  
The G format is almost useless as is (and always has been), so if it
could be made useful for reading csv files, why not?

Hopefully, it will be straightforward to both read and write this kind
of file (e.g. with columns separated optionally by tabs).  This ability
is long, long, overdue in fortran.  We should have been able to do this
transparently for the past 20 years.

$.02 -Ron Shepard



Sat, 30 Jul 2011 07:41:40 GMT  
 Fortran 77: read second number on a line in a comma delimited text file

Quote:


> ...
> > Is there a way to use format directed input in fortran 77 to read the
> > second number of each line in a comma delimited text file (.csv file)
> It's hard to say it's "more elegant" owing to the limitations it
> imposes, but certainly for the data shown one could use

>     read(infile,'(2X,I1)') x(i)

The OP has a file with numbers separated by commas.
A formatted read won't do.

Quote:
> The obvious problem is that for anything but precisely the one-column
> width of the integers between 0-9 the read fails whereas the free format
> handles it automagically.

> The elegant way would be to read the line as a character and parse it,
> but that elegance comes at the price of more complex coding.

That is not elegant to say the least.
Simplest and neatest is what the OP wrote.


Sat, 30 Jul 2011 23:31:47 GMT  
 Fortran 77: read second number on a line in a comma delimited text file
Quote:



>> ...
>>> Is there a way to use format directed input in fortran 77 to read the
>>> second number of each line in a comma delimited text file (.csv file)

>> It's hard to say it's "more elegant" owing to the limitations it
>> imposes, but certainly for the data shown one could use

>>     read(infile,'(2X,I1)') x(i)

> The OP has a file with numbers separated by commas.
> A formatted read won't do.

...
Of course it will w/ the given caveats it will be restricted to the
particular field width chosen, etc., etc, etc., ...

The given data was a N,N,N where "N" was a single digit 0-9.  The 2X
will skip the initial "N," portion of the record, the I1 will read the
second "N".

As noted, it fails for any data line which doesn't follow the model.

As for "elegance" of the more full-blown, generic parsing solution, I
was presuming enough sophistication in the reader to infer that would
only be so for the more generic question of dealing w/ csv files in
general, but gather that was too much to have hoped for... :(

--



Sat, 30 Jul 2011 23:48:48 GMT  
 Fortran 77: read second number on a line in a comma delimited text file

Quote:


> > The OP has a file with numbers separated by commas.
> > A formatted read won't do.
> ...
> Of course it will w/ the given caveats it will be restricted to the
> particular field width chosen, etc., etc, etc., ...

I don't see Robin's posts directly, partly in order to remove what would
otherwise be the temptation to respond to the numerous misstatements
pervading them. Most times that I see a portion quoted, I'm reminded of
this.

In this case, since the data is in a text file, a formatted read is the
only option other than nonstandard, system-dependent tricks. One might
use an explicit format as dpb notes, use list-directed formatting, or
read the data in character form for subsequent internal parsing. All of
those are formatted reads.

--
Richard Maine                    | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle           |  -- Mark Twain



Sun, 31 Jul 2011 00:38:00 GMT  
 
 [ 54 post ]  Go to page: [1] [2] [3] [4]

 Relevant Pages 

1. VW code to read comma-delimited text files??

2. Import comma delimited text file

3. Reading a comma delimited file

4. Export Clarion .DAT files to ASCII comma delimited files

5. Export Clarion .DAT files to ASCII comma delimited files

6. need fast parser for comma/space delimited numbers

7. DejaNews down (was RE: need fast parser for comma/space delimited numbers)

8. getting fields NOT comma delimited with commas inside

9. fortran program reading an external text (and numbers) file and writing with edits

10. AWK & Comma Delimited Text

11. Reading Comma delimited, Quoted String records

12. Fomatted read from a csv file (FORTRAN 77)

 

 
Powered by phpBB® Forum Software