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