getting fileinput to do errors='ignore' or 'replace'?

Adam Funk a24061 at ducksburg.com
Thu Dec 3 14:17:32 EST 2015


On 2015-12-03, Peter Otten wrote:

> def my_hook_encoded(encoding, errors=None):
>     import io
>     def openhook(filename, mode):
>         mode = mode.replace('U', '').replace('b', '') or 'r'
>         return io.open(
>             filename, mode, 
>             encoding=encoding, newline='', 
>             errors=errors)
>     return openhook
>
> for line in fileinput.input(
>         options.files,
>         openhook=my_hook_encoded("utf-8", errors="ignore")):
>     do_stuff(line)

Perfect, thanks!


> (codecs.open() instead of io.open() should also work)

OK.


-- 
The internet is quite simply a glorious place. Where else can you find
bootlegged music and films, questionable women, deep seated xenophobia
and amusing cats all together in the same place?       --- Tom Belshaw



More information about the Python-list mailing list