PyNGL Home > Functions > File IO


Reads data from an ASCII file.


array = Ngl.asciiread(filename, dims, type='float', sep=',')



The name of the ASCII file to read.


A list or tuple specifying the dimension sizes (or -1, see below).


An optional argument specifying the type of the data you are reading. The legal values are: 'integer', 'float', or 'double'.

sep=', '

An optional argument specifying a field separator, in addition to white space. This argument may be a single character or a character sequence. The algorithm is to replace all occurrences of "sep" in the input file with blanks and then collect all appropriate numeric values separated by white space.

This argument was added in version 1.3.0.

Return value


A NumPy array of dims dimensions and of type type.


This function reads numeric data from an ASCII file and returns a NumPy array of a specified shape. The algorithm employed is to continue reading data items into a 1-dimensional NumPy array until the required number have been read, then reshape the array and return it.

filename is the file name of the ASCII file to read from, dims is a Python list, or tuple, that specifies the desired shape of the output array, and type specifies the type of the data to be read. Type must be one of "integer", "float", or "double". This function ignores all tokens in the input file that are not of the specified type. If dims is specified as a negative one (-1), then all values in the input file of the specified type are stuffed into a 1D NumPy array and returned. If the type is not specified it defaults to "float".