Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Malcolm Greene Newsgroups: comp.lang.python Subject: Re: Capturing the bad codes that raise UnicodeError exceptions during decoding Date: Thu, 04 Aug 2016 15:22:48 -0400 Lines: 30 Message-ID: References: <1470336426.893449.686225577.24458777@webmail.messagingengine.com> <1470338568.900965.686261921.548DDA14@webmail.messagingengine.com> Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de VpKlwpyGHWGveFf9Inq5hAuWhgPjb5SpJP2BP/mFUsiQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'finished.': 0.07; 'utf-8': 0.07; 'csv': 0.09; 'received:internal': 0.09; 'suggestions.': 0.09; 'python': 0.10; 'def': 0.13; 'subject: \n ': 0.15; '(assuming': 0.16; '(via': 0.16; '*before*': 0.16; '3):': 0.16; 'chris,': 0.16; 'decode': 0.16; 'from:addr:python': 0.16; 'handling.': 0.16; 'malcolm': 0.16; 'mean,': 0.16; 'message- id:@webmail.messagingengine.com': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:10.202.2.212': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:io': 0.16; 'received:messagingengine.com': 0.16; 'received:psf.io': 0.16; 'replaced.': 0.16; 'row': 0.16; 'bytes': 0.18; 'text,': 0.22; 'header:In-Reply-To:1': 0.24; 'sense': 0.26; 'error': 0.27; 'actual': 0.28; 'subject:that': 0.29; 'them?': 0.29; 'code': 0.30; 'raising': 0.33; 'file': 0.34; 'behind': 0.35; 'text': 0.35; 'done': 0.35; 'unicode': 0.35; 'something': 0.35; 'level': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'thanks': 0.37; 'received:66': 0.38; 'data': 0.39; 'sure': 0.39; 'subject:the': 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'your': 0.60; "you'll": 0.61; 'skip:u 10': 0.61; 'header:Message-Id:1': 0.61; 'total': 0.62; 'within': 0.64; 'goal': 0.64; 'capture': 0.66; 'frequency': 0.66; 'quality': 0.72; "that'll": 0.84; 'upstream': 0.84 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=bdurham.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=w6T8wtJE/tVksqjPdAkATOhH7rA=; b=bays1b hPLvGyo7kLfXIQSylp27q14/4zfyvXfutPGKrdA06Ytu3pdeg74Wmqfhx5HWqal6 hj37yXLBltqsUMylImAVYUIW8M/QbYMB00TOKxE+5jZHaexkLN21y0ZQjbW6ZBo9 utx2DckEUyCfauDAr9Y55i68QvqK/v3h7GP5Q= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=w6T8wtJE/tVksqj PdAkATOhH7rA=; b=utD2ywzgFN8glhrLVuJ8cVyMFmtatSy2Z2DXuP5n175U/jO lH02rp+n+8Kigf6G6seh/Q/rmssy62r152n7CNGdevjiwkeqe8mmZZMa8mUQPtzk YhmK6rjXgnZpVvfrd5dwQFuXalPLYIvpEjZmlrwZgKBYXW0v+AWI7zr3el78= X-Sasl-Enc: jsayOouhjLk9FhzKTYcV6aY9dl/DO/Mu70FNjwRozqlt 1470338568 X-Mailer: MessagingEngine.com Webmail Interface - ajax-b9085e99 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <1470338568.900965.686261921.548DDA14@webmail.messagingengine.com> X-Mailman-Original-References: <1470336426.893449.686225577.24458777@webmail.messagingengine.com> Xref: csiph.com comp.lang.python:112346 Hi Chris, Thanks for your suggestions. I would like to capture the specific bad codes *before* they get replaced. So if a line of text has 10 bad codes (each one raising UnicodeError), I would like to track each exception's bad code but still return a valid decode line when finished. My goal is to count the total number of UnicodeExceptions within a file (as a data quality metric) and track the frequency of specific bad code's (via a collections.counter dict) to see if there's a pattern that can be traced to bad upstream process. Malcolm Remove them? Not sure what you mean, exactly; but would an errors="backslashreplace" decode do the job? Something like (assuming you use Python 3): def read_dirty_file(fn): with open(fn, encoding="utf-8", errors="backslashreplace") as f: for row in csv.DictReader(f): process(row) You'll get Unicode text, but any bytes that don't make sense in UTF-8 will be represented as eg \x80, with an actual backslash. Or use errors="replace" to hide them all behind U+FFFD, or other forms of error handling. That'll get done at a higher level than the CSV reader, like you suggest.