On Wed, Feb 10, 2021 at 11:14 PM Anders Munch <a...@flonidan.dk> wrote: > > 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? >
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 <songofaca...@gmail.com> _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/XPBVG5GU37UDQPDTZIFIGI2WOFYHYQBU/ Code of Conduct: http://python.org/psf/codeofconduct/