Parsing a CSV-format data file
|
|
|
| Type: | Programming |
| Rating: | 2 (easy-medium) |
| Description: | Parsing a CSV-format data file line by line.
|
| File(s): | readcsv.mos |
|
|
|
| readcsv.mos |
(!*******************************************************
* Mosel Example Problems *
* ====================== *
* *
* file readcsv.mos *
* ```````````````` *
* Example for the use of the Mosel language *
* (Read a data file in CSV format) *
* *
* (c) 2014 Fair Isaac Corporation *
* author: Y. Colombani *
*******************************************************!)
model readcsv
uses 'mmsystem'
declarations
public example_file=`
1|Estaque|1/2/1980|13.2
2|Endoume|23/5/2001|-77.3
23|wrongly formatted line
3|Lodi|12/12/1999|1
`
l:text
ndx:integer
word:text
when:date
number:real
end-declarations
fopen("text:example_file",F_INPUT)
setparam("sys_sepchar",getchar("|",1)) ! field separator
setparam("datefmt","%d/%m/%y") ! date format
while (readtextline(l)>0) do
trim(l,SYS_RIGHT) ! remove end of line character
setparam("sys_endparse",0) ! start parsing
b:=nextfield(l) ! jump to first field
ndx:=parseint(l); b:=b and nextfield(l) ! parse an integer,
word:=parsetext(l); b:=b and nextfield(l) ! some text,
parseextn(l,when); b:=b and nextfield(l) ! a date,
number:=parsereal(l) ! and a number
if not b then
writeln("!! Line not properly formatted")
else
writeln("->",ndx,"\t",word,"\t",when,"\t",number)
end-if
end-do
end-model
|
© 2001-2020 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.
