Guessing the encoding from a BOM
Ben Finney
ben+python at benfinney.id.au
Wed Jan 15 22:47:00 EST 2014
Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:
> enc = guess_encoding_from_bom("filename")
> if enc == something:
> # Can't guess, fall back on an alternative strategy
> ...
> else:
> f = open("filename", encoding=enc)
>
>
> If I forget to check the returned result, I should get an explicit
> failure as soon as I try to use it, rather than silently returning the
> wrong results.
Yes, agreed.
> What should I return as the default default? I have four possibilities:
>
> (1) 'undefined', which is an standard encoding guaranteed to
> raise an exception when used;
+0.5. This describes the outcome of the guess.
> (2) 'unknown', which best describes the result, and currently
> there is no encoding with that name;
+0. This *better* describes the outcome, but I don't think adding a new
name is needed nor very helpful.
> (3) None, which is not the name of an encoding; or
−1. This is too much like a real result and doesn't adequately indicate
the failure.
> (4) Don't return anything, but raise an exception. (But
> which exception?)
+1. I'd like a custom exception class, sub-classed from ValueError.
--
\ “I love to go down to the schoolyard and watch all the little |
`\ children jump up and down and run around yelling and screaming. |
_o__) They don't know I'm only using blanks.” —Emo Philips |
Ben Finney
More information about the Python-list
mailing list