IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
4.8.1.5 StreamReader Objects


4.8.1.5 StreamReader Objects

The StreamReader class is a subclass of Codec and defines the following methods which every stream reader must define in order to be compatible with the Python codec registry.

class StreamReader( stream[, errors])
Constructor for a StreamReader instance.

All stream readers must provide this constructor interface. They are free to add additional keyword arguments, but only the ones defined here are used by the Python codec registry.

stream must be a file-like object open for reading (binary) data.

The StreamReader may implement different error handling schemes by providing the errors keyword argument. These parameters are defined:

  • 'strict' Raise ValueError (or a subclass); this is the default.
  • 'ignore' Ignore the character and continue with the next.
  • 'replace' Replace with a suitable replacement character.

The errors argument will be assigned to an attribute of the same name. Assigning to this attribute makes it possible to switch between different error handling strategies during the lifetime of the StreamReader object.

The set of allowed values for the errors argument can be extended with register_error().

read( [size[, chars, [firstline]]])
Decodes data from the stream and returns the resulting object.

chars indicates the number of characters to read from the stream. read() will never return more than chars characters, but it might return less, if there are not enough characters available.

size indicates the approximate maximum number of bytes to read from the stream for decoding purposes. The decoder can modify this setting as appropriate. The default value -1 indicates to read and decode as much as possible. size is intended to prevent having to decode huge files in one step.

firstline indicates that it would be sufficient to only return the first line, if there are decoding errors on later lines.

The method should use a greedy read strategy meaning that it should read as much data as is allowed within the definition of the encoding and the given size, e.g. if optional encoding endings or state markers are available on the stream, these should be read too.

Changed in version 2.4: chars argument added. Changed in version 2.4.2: firstline argument added.

readline( [size[, keepends]])
Read one line from the input stream and return the decoded data.

size, if given, is passed as size argument to the stream's readline() method.

If keepends is false line-endings will be stripped from the lines returned.

Changed in version 2.4: keepends argument added.

readlines( [sizehint[, keepends]])
Read all lines available on the input stream and return them as a list of lines.

Line-endings are implemented using the codec's decoder method and are included in the list entries if keepends is true.

sizehint, if given, is passed as the size argument to the stream's read() method.

reset( )
Resets the codec buffers used for keeping state.

Note that no stream repositioning should take place. This method is primarily intended to be able to recover from decoding errors.

In addition to the above methods, the StreamReader must also inherit all other methods and attributes from the underlying stream.

The next two base classes are included for convenience. They are not needed by the codec registry, but may provide useful in practice.

See About this document... for information on suggesting changes.