Navigation
Synopsis Write a relation to a CSV (Comma Separated Values) file.
Function
  1. void writeCSV(&T relation, loc location, bool header = true, str separator = ",", str encoding = "UTF8")
  2. void writeCSV(&T relation, loc location, map[str,str] options)
Usage import lang::csv::IO;
Description Write relation to a CSV file at location. The options influence the way the actrual CSV file is written:
  • header: add or omit a header (based on the labels of the relation).
  • separator: defines the separator character between fields (default is ,).
Examples
rascal>import lang::csv::IO;
ok
rascal>rel[int position, str artist, str title, int year] R1 = {
>>>>>>>  <1,"Eagles","Hotel California",1977>,
>>>>>>>  <2,"Queen","Bohemian rhapsody",1975>,
>>>>>>>  <3,"Boudewijn de Groot","Avond",1997>
>>>>>>>};
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>
}
rascal>writeCSV(R1, |courses:///Rascal/Libraries/lang/csv/ex1a.csv|);
ok
rascal>writeCSV(R1, |courses:///Rascal/Libraries/lang/csv/ex1b.csv|, header = false, separator = ";");
ok
will produce the following files:

ex1a.csv (with a header line and default separator ,):
position,artist,title,year
1,Eagles,Hotel California,1977
3,Boudewijn de Groot,Avond,1997
2,Queen,Bohemian rhapsody,1975
ex1b.csv (without a header line with separator ;):
1;Eagles;Hotel California;1977
3;Boudewijn de Groot;Avond;1997
2;Queen;Bohemian rhapsody;1975
Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.