18

I'm writing some mail-processing software in Python that is encountering strange bytes in header fields. I suspect this is just malformed mail; the message itself claims to be us-ascii, so I don't think there is a true encoding, but I'd like to get out a unicode string approximating the original one without throwing a UnicodeDecodeError.

So, I'm looking for a function that takes a str and optionally some hints and does its darndest to give me back a unicode. I could write one of course, but if such a function exists its author has probably thought a bit deeper about the best way to go about this.

I also know that Python's design prefers explicit to implicit and that the standard library is designed to avoid implicit magic in decoding text. I just want to explicitly say "go ahead and guess".

Nick
  • 20,304
  • 18
  • 46
  • 50

3 Answers3

27

+1 for the chardet module (suggested by @insin).

It is not in the standard library, but you can easily install it with the following command:

$ pip install chardet

Example:

>>> import chardet
>>> import urllib
>>> detect = lambda url: chardet.detect(urllib.urlopen(url).read())
>>> detect('http://stackoverflow.com')
{'confidence': 0.85663169917190185, 'encoding': 'ISO-8859-2'}    
>>> detect('https://stackoverflow.com/questions/269060/is-there-a-python-lib')
{'confidence': 0.98999999999999999, 'encoding': 'utf-8'}

See Installing Pip if you don't have one.

Community
  • 1
  • 1
jfs
  • 374,366
  • 172
  • 933
  • 1,594
  • 1
    Didn't it strike you that `ISO-8859-2` was a nonsense? – John Machin Aug 28 '10 at 02:05
  • @John Machin: Yes, it was. It is educational to show that you should not blindly trust it. Current results are different ('utf-8' and 'ascii' correspondingly). – jfs Aug 28 '10 at 06:41
14

As far as I can tell, the standard library doesn't have a function, though it's not too difficult to write one as suggested above. I think the real thing I was looking for was a way to decode a string and guarantee that it wouldn't throw an exception. The errors parameter to string.decode does that.

def decode(s, encodings=('ascii', 'utf8', 'latin1')):
    for encoding in encodings:
        try:
            return s.decode(encoding)
        except UnicodeDecodeError:
            pass
    return s.decode('ascii', 'ignore')
Nick
  • 20,304
  • 18
  • 46
  • 50
  • 1
    You could skip the `'ascii'` case at the end and just use `latin1`, since `latin1` will decode all 256 byte values without error. – Mark Ransom Apr 03 '17 at 15:50
1

The best way to do this that I've found is to iteratively try decoding a prospective with each of the most common encodings inside of a try except block.

Jeremy Cantrell
  • 24,765
  • 13
  • 53
  • 78