1

The documentation for open states:

encoding is the name of the encoding used to decode or encode the
file. This should only be used in text mode.  The default encoding is
platform dependent, but any encoding supported by Python can be
passed.  See the codecs module for the list of supported encodings.

So I import codecs and then what to understand what I can pass in as the encoding parameter to open?

Dave
  • 7,555
  • 8
  • 46
  • 88

2 Answers2

1
help(codecs)

list the help for encode() as well.

The following docs page also lists out the supported encodings. https://docs.python.org/3/library/codecs.html

inverzeio
  • 525
  • 2
  • 10
1

Not trivial -

For some reason, there is no way to list the codecs from a Python interpreter.

The codecs module allows one to lookup for a registered codec, by using

import codecs

codecs.lookup("codec_name")

But the codec registry is an private list in the interpreter core, that is not exposed to Python only code.

The lookup code is at: https://github.com/python/cpython/blob/673c39331f844a80c465efd7cff88ac55c432bfb/Python/codecs.c#L100

Trying to write a C simple function to return a copy of that internal list soon finds out that it is an struct member in the InterpreterState that is reserved for internal use, and should not be touched by external code.

So, you can only guess at codec names, and use the "lookup" function above.

jsbueno
  • 99,910
  • 10
  • 151
  • 209