barak.io.readtxt¶
- barak.io.readtxt(fh, sep=None, usecols=None, comment='#', skip=0, arrays=True, names=None, readnames=False, converters=None, mintype=<type 'int'>)[source]¶
Reads columns from a text file into arrays, converting to int, float or str where appropriate.
By default the column separator is whitespace. rows can be either an input filename or an iterable (e.g. a file object, list or iterator).
Parameters : rows : filename or iterable object
Input data.
sep : str (default None)
A string used to separate items on a row (also known as a delimiter). Default is None, which means whitespace.
usecols : int or tuple of ints, optional
Indices of columns to be read. By default all columns are read.
comment : str (default #)
Character marking the start of a comment.
skip : int (default 0)
Number of rows to skip (not counting commented or blank lines) before reading data.
arrays : bool (True)
If True, all columns are converted to Numpy arrays. If False, columns are returned as lists.
names : str or sequence of str (default None)
If names is given and arrays is True, the data are placed in a Numpy record array with field names given by names. Can also be a single string of comma-separated values.
readnames : bool (False)
If readnames is True the first line of the file is read to find the field names. This overrides the names keyword.
converters : dict (None)
Functions to apply to each entry of a column. Each (key,value) pair gives the column index (key) and the function to be applied to each entry in that column (value).
Returns either structured array or lists. :
Examples
>>> list_of_all_cols = readtxt('filename') >>> ninthcol, fifthcol = readtxt('filename', sep=',', usecols=(8,4)]) >>> firstcol = readtxt('filename', comment='%', usecols=[0]) >>> recarray = readtxt('filename', sep=',', usecols=(1,3), names='x,y'])