Routines (alphabetical) > Routines: R > READ_CSV

READ_CSV

Syntax | Return Value | Arguments | Keywords | Version History | See Also

The READ_CSV function reads data from a “comma-separated value” (comma-delimited) text file into an IDL structure variable.

This routine handles CSV files consisting of one or more optional table header lines, followed by one optional column header line, followed by columnar data, with commas separating each field. Each row is assumed to be a new record. Blank lines at the beginning of the file are automatically ignored.

This routine is written in the IDL language. Its source code can be found in the file read_csv.pro in the lib subdirectory of the IDL distribution.

Syntax

Result = READ_CSV( Filename [, COUNT=variable] [, HEADER=variable] [, MISSING_VALUE=value] [, N_TABLE_HEADER=value] [, NUM_RECORDS=value] [, RECORD_START=value] [, TABLE_HEADER=variable])

Return Value

Returns a structure variable that contains one structure field for each column of data in the CSV file. The data type of the individual structure fields is determined using the following rules:

Resulting Data Type

If this condition is met

Long integer

All data within the column consists of integers, all of which are smaller than the largest 32-bit signed integer.

64-bit Long integer

All data within the column consists of integers, at least one of which is greater than the largest 32-bit signed integer.

Double-precision floating point integer

All data within the column consists of numbers, at least one of which has a decimal point or exponent.

String

Any column that does not meet one of the above conditions.

Arguments

Filename

A string containing the name of a CSV file to be read.

Keywords

COUNT

Set this keyword equal to a named variable that will contain the number of records read.

HEADER

Set this keyword equal to a named variable that will contain the column headers as a vector of strings. If no header exists, an empty scalar string is returned.

Note: The headers will only be returned for CSV files that contain numeric data. If the CSV file contains only string data, the header (if it exists) will be contained in the first data record.

MISSING_VALUE

Set this keyword equal to a value used to replace any missing floating-point or integer data. The default value is 0.

N_TABLE_HEADER

Set this keyword equal to the number of lines of the CSV file to skip before beginning to read records from the file. (If a column header line is present, it will not be skipped.) If the TABLE_HEADER keyword specifies a variable to hold table header information, each skipped line will be stored as a single string in that variable.

If the HEADER keyword is also present, the column headers will be taken from the line following the line specified by N_TABLE_HEADER.

NUM_RECORDS

Set this keyword equal to an integer specifying the number of records to read. By default all records are read.

RECORD_START

Set this keyword equal an integer specifying the index of the first record to read. The default is the first record of the file (record 0).

TABLE_HEADER

Set this keyword equal to a named variable that will contain the file's table header information as a vector of strings. The number of lines specified by the N_TABLE_HEADER keyword will be interpreted as the table header. Table header information appears in the CSV file above the column headers specified by the HEADER keyword (if present).

Version History

7.1

Introduced

8.0

N_TABLE_HEADER and TABLE_HEADER keywords added

See Also

IOPEN, QUERY_ASCII, QUERY_CSV, READ_ASCII , WRITE_CSV