Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #112347
| Path | csiph.com!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Capturing the bad codes that raise UnicodeError exceptions during decoding |
| Date | Thu, 4 Aug 2016 20:33:12 +0100 |
| Lines | 51 |
| Message-ID | <mailman.175.1470339200.6033.python-list@python.org> (permalink) |
| References | <1470336426.893449.686225577.24458777@webmail.messagingengine.com> <CAPTjJmp-RCZB2J+GVdVNVM56Nhe-4y9GL9MWW+L-RnpNGziFfw@mail.gmail.com> <1470338568.900965.686261921.548DDA14@webmail.messagingengine.com> <fa6f6ec9-bf33-f891-959e-91cf945990b8@mrabarnett.plus.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de 7CnTiusdtqQeDrlOqiQuUgm1QELTF4uWqk/BW0Wn9iog== |
| Return-Path | <python@mrabarnett.plus.com> |
| 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; 'else:': 0.03; 'finished.': 0.07; 'utf-8': 0.07; 'csv': 0.09; 'suggestions.': 0.09; 'python': 0.10; 'exception': 0.13; 'def': 0.13; 'subject: \n ': 0.15; '(assuming': 0.16; '(via': 0.16; '*before*': 0.16; '3):': 0.16; 'attributes:': 0.16; 'chris,': 0.16; 'decode': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'handling.': 0.16; 'malcolm': 0.16; 'mean,': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'replaced.': 0.16; 'row': 0.16; 'wrote:': 0.16; 'bytes': 0.18; 'try:': 0.18; 'text,': 0.22; 'header:In- Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'sense': 0.26; 'error': 0.27; 'actual': 0.28; 'subject:that': 0.29; 'them?': 0.29; 'code': 0.30; 'received:84': 0.32; 'raising': 0.33; 'file': 0.34; 'except': 0.34; 'behind': 0.35; 'could': 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; 'thanks': 0.37; 'data': 0.39; 'sure': 0.39; 'subject:the': 0.39; 'received:192': 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; '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 |
| X-CM-Score | 0.00 |
| X-CNFS-Analysis | v=2.2 cv=DaJVkblW c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=IkcTkHD0fZMA:10 a=gxB-0CnM_h_gR0dx134A:9 |
| X-AUTH | mrabarnett@:2500 |
| User-Agent | Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 |
| In-Reply-To | <1470338568.900965.686261921.548DDA14@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 <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <fa6f6ec9-bf33-f891-959e-91cf945990b8@mrabarnett.plus.com> |
| X-Mailman-Original-References | <1470336426.893449.686225577.24458777@webmail.messagingengine.com> <CAPTjJmp-RCZB2J+GVdVNVM56Nhe-4y9GL9MWW+L-RnpNGziFfw@mail.gmail.com> <1470338568.900965.686261921.548DDA14@webmail.messagingengine.com> |
| Xref | csiph.com comp.lang.python:112347 |
Show key headers only | View raw
On 2016-08-04 20:22, Malcolm Greene wrote:
> 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.
>
You could catch the UnicodeDecodeError exception and look at its attributes:
try:
b'\x80'.decode('utf-8')
except UnicodeDecodeError as e:
print('Failed to decode')
print('e.start is', e.start)
print('e.end is', e.end)
else:
print('Decoded successfully')
It prints:
Failed to decode
e.start is 0
e.end is 1
> Malcolm
>
> <snipped>
> 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.
> </snipped>
>
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Capturing the bad codes that raise UnicodeError exceptions during decoding MRAB <python@mrabarnett.plus.com> - 2016-08-04 20:33 +0100
csiph-web