![]() |
|
Navigation |
Synopsis Read a relation from a CSV (Comma Separated Values) file.
Function
Usage
import lang::csv::IO;
Description Read a CSV file and return a value of a required type.
The result argument is the required type of the value that is produced by reading the CSV
that is found at location .
Optionally, the following arguments can be supplied:
Step 1: The type of each field occurrence is inferred from its contents using the following rules:
Reading the values in fields is straightforward, except for the case that the text in the field is enclosed between double quotes ( " ):
Examples Given is the follwing file
ex1.csv :
position;artist;title;year 1;Eagles;Hotel California;1977 2;Queen;Bohemian rhapsody;1975 3;Boudewijn de Groot;Avond;1997We can read it in various ways: rascal>import lang::csv::IO; ok rascal>R1 = readCSV(#rel[int position, str artist, str title, int year], |courses:///Rascal/Libraries/lang/csv/ex1.csv|, separator = ";"); rel[int position,str artist,str title,int year]: { <1,"Eagles","Hotel California",1977>, <3,"Boudewijn de Groot","Avond",1997>, <2,"Queen","Bohemian rhapsody",1975> }Now we can, for instance, select one of the fields of R1 :
rascal>R1.artist;
set[str]: {"Queen","Boudewijn de Groot","Eagles"}
It is also possible to infer the type:
rascal>R1 = readCSV(|courses:///Rascal/Libraries/lang/csv/ex1.csv|, separator = ";");
readCSV inferred the relation type: rel[int \position,str \artist,str \title,int \year]
value: {
<1,"Eagles","Hotel California",1977>,
<3,"Boudewijn de Groot","Avond",1997>,
<2,"Queen","Bohemian rhapsody",1975>
}
![]() |