Python3: Sane way to deal with broken encodings

Benjamin Kaplan benjamin.kaplan at case.edu
Mon Dec 7 14:42:13 EST 2009


On Mon, Dec 7, 2009 at 2:16 PM, Johannes Bauer <dfnsonfsduifb at gmx.de> wrote:
> Bruno Desthuilliers schrieb:
>
>>> Is that possible? If so, how?
>>
>> This might get you started:
>>
>> """
>>>>> help(str.decode)
>> decode(...)
>>     S.decode([encoding[,errors]]) -> object
>
> Hmm, this would work nicely if I called "decode" explicitly - but what
> I'm doing is:
>
> #!/usr/bin/python3
> for line in open("broken", "r"):
>        pass
>
> Which still raises the UnicodeDecodeError when I do not even do any
> decoding explicitly. How can I achieve this?
>
> Kind regards,
> Johannes
>

Looking at the python 3 docs, it seems that open takes the encoding
and errors parameters as optional arguments. So you can call
open('broken', 'r',errors='replace')

> --
> "Aus starken Potentialen können starke Erdbeben resultieren; es können
> aber auch kleine entstehen - und "du" wirst es nicht für möglich halten
> (!), doch sieh': Es können dabei auch gar keine Erdbeben resultieren."
> -- "Rüdiger Thomas" alias Thomas Schulz in dsa über seine "Vorhersagen"
> <1a30da36-68a2-4977-9eed-154265b17d28 at q14g2000vbi.googlegroups.com>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list