CSV files

Read and write

UCBLogo has no built-in support for generic CSV files, I have developed three functions (see the documentation in csv.pdf ) to read and write them following as far as possible RFC 4180 “Common Format and MIME Type for Comma-Separated Values (CSV) Files”. The csv module requires the dataframe module (see the next section) and two utility modules: string functions and regular sequences (see the documentation in str.pdf and rs.pdf ) .

csv.read

Reads a CSV file one character at a time and outputs a list of lists. When data types are not defined each sublist represents a row in the CSV file. Otherwise returns a dataframe, defined as a list composed of two lists: a first list of data types (“char” for character variables, “num” for numeric variables) and a second one composed of sentences (lists of words) representing the variables in the dataframe, where in each sentence the first word is the variable name and the others its values. If types are defined, non numeric values in a numeric columnn in the CSV file are set to missing as empty fields in a numeric or character column.

Examples

Reads visits.csv data file: show csv.read “visits.csv

[[PATIENT_ID VISIT_DATE SCORE] [2002 2020-09-10 6.8] [1001 2020-09-17 5.5] [4004 2020-09-24 8.4] [2002 2020-10-08 ] [1001  6.6] [3003 2020-11-12 ] [4004 2020-11-05 7.0] [1001 2020-11-19 5.3]]

Reads “visits.csv” returnig a dataframe: show (csv.read “visits.csv [char char num])

[[char char num] [[PATIENT_ID 2002 1001 4004 2002 1001 3003 4004 1001] [VISIT_DATE 2020-09-10 2020-09-17 2020-09-24 2020-10-08 missing 2020-11-12 2020-11-05 2020-11-19] [SCORE 6.8 5.5 8.4 missing 6.6 missing 7.0 5.3]]]

csv.write

Writes a csv file from a list of lists.

Examples

make "list csv.transpose (list se "id iseq 1 20 se "sex (rs.rep [female male] 10) se "age rs.rand 1 100 20)  
csv.write  :list "list.csv  

make "df list [num char num] csv.transpose :list  
csv.write  :df "df.csv  

csv.read.lines

Reads a simple csv file line by line, makes it possible to progressively calculate summary statistics while reading each line of a CSV file (see for an example the “NYC Marathon” section).

Examples

make "out csv.transpose (list se "id iseq 1 20 se "sex (rs.rep [female male] 10) se "age rs.rand 1 100 20)  
csv.write  :out "list.csv  

make "list csv.read.lines "list.csv