mindspore.dataset.utils.LineReader

class mindspore.dataset.utils.LineReader(filename)[source]

Line-based file reader.

Cache the line-based meta data of the file in advance to achieve random-access reading of each file line.

Parameters

filename (str) – Filename to be read.

Raises
  • TypeError – If filename is not of type int.

  • RuntimeError – If filename does not exist or is not a regular file.

Examples

>>> from mindspore.dataset import LineReader
>>>
>>> reader = LineReader("/path/to/txt/or/csv/file")
>>> # Read the first line of csv file
>>> reader.readline(1)
>>> # Return the row size in csv file
>>> reader.len()
>>> # Close the handle
>>> reader.close()
close()[source]

Close the file handle.

len()[source]

Get the total number of lines in the current file.

readline(line)[source]

Reads the contents of the specified line.

Parameters

line (int) – The line number to be read, with a starting line number of 1.

Returns

str, the contents of the corresponding line, without line break characters.

Raises
  • TypeError – If line is not of type int.

  • ValueError – If line exceeds the total number of lines in the file.