mindspore.dataset.utils.LineReader

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

Efficient file line reader.

This class is used to hold the line offsets of line based file.

The following functionality is provided:

  • len(): return the number of lines in the file

  • readline(line): open file handle (if not opened yet), and read a line in the file

  • close(): close the file handle opened in readline

Parameters

filename (str) – line based file to be loaded.

Raises
  • TypeError – Parameter filename is wrong.

  • RuntimeError – The input file does not exist or is not a regular file.

Examples

>>> from mindspore.dataset import LineReader
>>>
>>> reader = LineReader("/path/to/txt/or/csv/file")
>>> reader.readline(1)
>>> reader.close()
close()[source]

Close the file

len()[source]

Get the total number of lines in the file

readline(line)[source]

Read specified line content.

Parameters

line (int) – the line number to be read, starting at 1.

Returns

str, line content (until line break character).

Raises
  • TypeError – Parameter line is the wrong type.

  • ValueError – Parameter line exceeds the file range.