Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: getting fileinput to do errors='ignore' or 'replace'? Date: Thu, 03 Dec 2015 17:11:37 +0100 Organization: None Lines: 57 Message-ID: References: <8336jcxi2m.ln2@news.ducksburg.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de iMw5iP+JIoyquJg9zNylxgrv2jXL/ZPLHukE/g3rALWw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; "'')": 0.07; "subject:' ": 0.07; 'subject:getting': 0.07; 'utf-8': 0.07; 'effect.': 0.09; 'filename,': 0.09; 'mode,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:ignore': 0.09; 'python': 0.10; 'template': 0.11; 'def': 0.13; 'ignore': 0.14; 'encoding': 0.15; 'skip:f 30': 0.15; "'r'": 0.16; '2.7.3': 0.16; 'adam': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'input': 0.18; '(on': 0.22; 'function:': 0.22; 'mixed': 0.22; 'forgot': 0.23; 'import': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'function': 0.28; "skip:' 10": 0.28; "i'm": 0.30; "i'd": 0.31; 'option': 0.31; 'another': 0.32; 'source': 0.33; 'file': 0.34; 'this?': 0.34; 'trouble': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'possible': 0.36; 'mode': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'files': 0.38; 'skip:o 20': 0.38; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'some': 0.40; 'your': 0.60; 'different': 0.63 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd8a41.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99966 Adam Funk wrote: > On 2015-12-03, Adam Funk wrote: > >> I'm having trouble with some input files that are almost all proper >> UTF-8 but with a couple of troublesome characters mixed in, which I'd >> like to ignore instead of throwing ValueError. I've found the >> openhook for the encoding >> >> for line in fileinput.input(options.files, >> openhook=fileinput.hook_encoded("utf-8")): >> do_stuff(line) >> >> which the documentation describes as "a hook which opens each file >> with codecs.open(), using the given encoding to read the file", but >> I'd like codecs.open() to also have the errors='ignore' or >> errors='replace' effect. Is it possible to do this? > > I forgot to mention: this is for Python 2.7.3 & 2.7.10 (on different > machines). Have a look at the source of fileinput.hook_encoded: def hook_encoded(encoding): import io def openhook(filename, mode): mode = mode.replace('U', '').replace('b', '') or 'r' return io.open(filename, mode, encoding=encoding, newline='') return openhook You can use it as a template to write your own factory function: 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) Another option is to create the function on the fly: for line in fileinput.input( options.files, openhook=functools.partial( io.open, encoding="utf-8", errors="replace")): do_stuff(line) (codecs.open() instead of io.open() should also work)