Skip to Content
Modern Python Standard Library Cookbook
book

Modern Python Standard Library Cookbook

by Alessandro Molina
August 2018
Intermediate to advanced
366 pages
10h 14m
English
Packt Publishing
Content preview from Modern Python Standard Library Cookbook

There's more...

If you're wondering how it might be possible to read a file for which the encoding is unknown, well, that's a far more complex problem.

The fact is that unless the file provides some guidance in a header, or something equivalent, that can tell you the type of encoding on the content, there is no reliable way to know how a file might be encoded.

You might try multiple different types of encoding and check which one is able to decode the content (doesn't throw UnicodeDecodeError), but the fact that a set of bytes decodes to an encoding doesn't guarantee that it decodes to the right result. For example, the 'ì' character encoded to utf-8 decodes perfectly in latin-1, but results in a totally different thing:

>>> 'ì'.encode('utf-8').decode('latin-1') ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Advanced Python Development: Using Powerful Language Features in Real-World Applications

Advanced Python Development: Using Powerful Language Features in Real-World Applications

Matthew Wilkes

Publisher Resources

ISBN: 9781788830829Supplemental Content