Read CSV

See also Write CSV

Function Syntax (LM:readcsv <csv>)
Current Version 1.3
Download ReadCSV-V1-3.lsp
View HTML Version ReadCSV-V1-3.html
Symbol Type Description
csv String Filename of CSV file to read
Type Description
List A list of lists where each sublist is a row of cell values

Program Description

This function will read an Excel Comma Separated Value (CSV) file and return a matrix list of the cell values, that is, a list of lists where each sublist is a row of cell values.

The function requires a single parameter: the full filename of the CSV file to be read. If the file can be successfully opened for reading, each row is parsed into a list of cell values; the list of rows is then returned.

Empty cells in the CSV file within the range of used cells will be returned as empty strings in the output list.

If the file cannot be opened for reading, or there is no data in the file, this function will return nil.

Test Program

The following program will present the user with a dialog interface, prompting to select a CSV file to be read. Following a valid selection, the program will print the list returned by the above LM:ReadCSV function to the command-line.

Select all
(defun c:test ( / data file )
            (setq file (getfiled "Select CSV File" "" "csv" 16))
            (setq data (LM:readcsv file))
            (princ "\n(")
            (foreach line data
                (princ "\n    ") (prin1 line)
            (princ "\n)")

Example of Output

Here is a demonstration of the above test program to give a quick example of the format of the list returned by the above LM:readcsv function.

Upon selecting a CSV file with the following content:


The LM:readcsv function will return the following list:

    ("CellA1" "CellB1" "CellC1" "CellD1")
    ("CellA2" "CellB2" "CellC2" "CellD2")
    ("CellA3" "CellB3" "CellC3" "CellD3")

See also Write CSV


increase · reset · decrease

Designed & Created by Lee Mac © 2010