Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: MRAB Newsgroups: comp.lang.python Subject: Re: getting fileinput to do errors='ignore' or 'replace'? Date: Thu, 3 Dec 2015 16:12:55 +0000 Lines: 30 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de SSfOwSGe3ZAcsA9swxqUQgyC8K/crsWSTPu0JcAFo/Xw== 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; "subject:' ": 0.07; 'subject:getting': 0.07; 'utf-8': 0.07; 'codecs': 0.09; 'effect.': 0.09; 'mode,': 0.09; 'subject:ignore': 0.09; 'def': 0.13; 'ignore': 0.14; 'encoding': 0.15; 'skip:f 30': 0.15; 'adam': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'input': 0.18; 'mixed': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.29; "i'm": 0.30; "i'd": 0.31; 'file': 0.34; 'this?': 0.34; 'trouble': 0.35; 'could': 0.35; 'but': 0.36; 'instead': 0.36; 'possible': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'files': 0.38; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'your': 0.60; 'opener': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=CvRCCSMD c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=EBOSESyhAAAA:8 a=IkcTkHD0fZMA:10 a=K72mLFHtHBaOnGtkPQQA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: 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:99967 On 2015-12-03 15:12, 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? > It looks like it's not possible with the standard "hook_encoded", but you could write your own alternative: import codecs def my_hook_encoded(encoding, errors): def opener(path, mode): return codecs.open(path, mode, encoding=encoding, errors=errors) return opener for line in fileinput.input(options.files, openhook=fileinput.my_hook_encoded("utf-8", "ignore")): do_stuff(line)