Re: PEP 597: Add optional EncodingWarning

Victor Stinner [mailto:vstinner@python.org] wrote:
encoding="utf8" is backward compatible and is likely to fix encoding bugs when the locale encoding is not UTF-8
This program runs just fine on 3.8.7 Windows, against a file.txt that contains latin-1 text: with open('file.txt', 'rt') as f: print(f.read()) But if I change it to this: with open('file.txt', 'rt', encoding='utf-8') as f: print(f.read()) then it fails with UnicodeDecodeError. How it that backwards compatible? regards, Anders

On Wed, Feb 10, 2021 at 11:14 PM Anders Munch <ajm@flonidan.dk> wrote:
There are several ways: * encoding="latin1" -- This is the best. Works perfectly. * Don't touch -- You don't need to enable EncodingWarning. * encoding=locale.getpreferredencoding(False) -- Backward compatible. But doesn't work if you enabled UTF-8 mode. * encoding="mbcs" -- Backward compatible. Works even when you enabled UTF-8 mode. But it doesn't work only on Windows. Regards, -- Inada Naoki <songofacandy@gmail.com>

On Wed, Feb 10, 2021 at 11:14 PM Anders Munch <ajm@flonidan.dk> wrote:
There are several ways: * encoding="latin1" -- This is the best. Works perfectly. * Don't touch -- You don't need to enable EncodingWarning. * encoding=locale.getpreferredencoding(False) -- Backward compatible. But doesn't work if you enabled UTF-8 mode. * encoding="mbcs" -- Backward compatible. Works even when you enabled UTF-8 mode. But it doesn't work only on Windows. Regards, -- Inada Naoki <songofacandy@gmail.com>
participants (2)
-
Anders Munch
-
Inada Naoki