Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Capturing the bad codes that raise UnicodeError exceptions during decoding Date: Fri, 5 Aug 2016 05:00:31 +1000 Lines: 25 Message-ID: References: <1470336426.893449.686225577.24458777@webmail.messagingengine.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de FarnVzLJbsJFWK3/NfT5igg7NvAbsff3H6yqI7lUHXbw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'utf-8': 0.07; 'cc:addr :python-list': 0.09; 'csv': 0.09; 'python': 0.10; 'def': 0.13; '(assuming': 0.16; '2016': 0.16; '3):': 0.16; 'decode': 0.16; 'decoding': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'handling.': 0.16; 'malcolm': 0.16; 'mean,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'row': 0.16; 'wrote:': 0.16; 'bytes': 0.18; 'passes': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'text,': 0.22; 'pass': 0.22; 'am,': 0.23; 'seems': 0.23; 'header:In-Reply-To:1': 0.24; 'sense': 0.26; 'appreciated.': 0.27; 'figure': 0.27; 'error': 0.27; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'errors.': 0.27; 'actual': 0.28; 'subject:that': 0.29; 'them?': 0.29; "i'm": 0.30; 'raising': 0.33; 'wrap': 0.33; 'received:google.com': 0.35; 'behind': 0.35; 'done': 0.35; 'unicode': 0.35; 'something': 0.35; 'level': 0.35; 'but': 0.36; 'subject:: ': 0.37; 'files': 0.38; 'sure': 0.39; 'subject:the': 0.39; 'skip:e 20': 0.39; 'high': 0.60; "you'll": 0.61; 'skip:u 10': 0.61; 'repeat': 0.67; 'chrisa': 0.84; "that'll": 0.84; 'to:none': 0.91; 'dirty': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:cc; bh=p7ub+AJ6B7QkWEgB89dykXvva6cW79GYISnIEYBtX0M=; b=A9LpuDn7bed1aZshM4oR7q1uNYMPqL3Id9Ezrxh8gdo2NUbRM2aUxdGudiQj0RkfVH fp5lh/aZy5oWw5QuavOot1Ssk0tob59Weta3Bc1rwwY62ZS+b5LR5xwPg3s9cSpeyzEb tY686boEwqlyefAuX9XJCfryAXLsPUXqB+Sq6cSSPtWrI5lnH4Sf7WbGFd4UTWH+8CRT KVRx7gC6lzvedOYYs18ohHNF9QY894fCiGjMhswOSmQm6rhg1VrQ9KdT7pTRwhYoFErt TagtK4LehsjYE0UI9j8mjsXDSzi+/1ux6HOzKA7LJ6vhTck8za4dlUMRGYCKofntc4Z3 g7RQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:cc; bh=p7ub+AJ6B7QkWEgB89dykXvva6cW79GYISnIEYBtX0M=; b=fYZlHtvNR7IMd8rRCmuxI5igvm/f9AQsbBRTi1tvaBIMnZCzDCQqfXPEbtgbpQxSyZ RNJ6ttJq7sqGVzl1DbAHTgTt9KH/eI+hx3YICuYel+lY1SkxQaXPXFTMMpW4OOPkav2n b4Gq2hEr7HWqdFcbTtEg/7wu0qGXLYcjp1fX/f69RuAcki7AfbwL3aEyZIXMNj5XY0Sm bwT7d/oghXX65VQRhNJM7qEFR/tzEQcDuMlX3dZsQy/18jt6d0+YUQiGK3iqzSV5552y 3atXpPzqpbEZXWaiWK75dqGIp0FDPhIBTw8tgth+66CV+dtk2zWwSG3YoXIrKl5sM9st aKRg== X-Gm-Message-State: AEkoousZE26L2aoHKRdtMyiBwlOZSKS45fMcrh9qDcDJ90Jicor6hgWqSvo4Uy8mx8qQQaRb/zO7BNebyGkXrw== X-Received: by 10.194.175.38 with SMTP id bx6mr74380399wjc.47.1470337231796; Thu, 04 Aug 2016 12:00:31 -0700 (PDT) In-Reply-To: <1470336426.893449.686225577.24458777@webmail.messagingengine.com> 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: X-Mailman-Original-References: <1470336426.893449.686225577.24458777@webmail.messagingengine.com> Xref: csiph.com comp.lang.python:112345 On Fri, Aug 5, 2016 at 4:47 AM, Malcolm Greene wrote: > I'm processing a lot of dirty CSV files and would like to track the bad > codes that are raising UnicodeErrors. I'm struggling how to figure out > what the exact codes are so I can track them, them remove them, and then > repeat the decoding process for the current line until the line has been > fully decoded so I can pass this line on to the CSV reader. At a high > level it seems that I need to wrap the decoding of a line until it > passes with out any errors. Any suggestions appreciated. 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. ChrisA