Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #26038
| Date | 2012-07-25 13:35 +0200 |
|---|---|
| From | Philipp Hagemeister <phihag@phihag.de> |
| Subject | Re: catch UnicodeDecodeError |
| References | <04f7ff8d-9881-4a04-ab2e-b5573b5f3cd1@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2570.1343216119.4697.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
Hi Jaroslav,
you can catch a UnicodeDecodeError just like any other exception. Can
you provide a full example program that shows your problem?
This works fine on my system:
import sys
open('tmp', 'wb').write(b'\xff\xff')
try:
buf = open('tmp', 'rb').read()
buf.decode('utf-8')
except UnicodeDecodeError as ude:
sys.exit("Found a bad char in file " + "tmp")
Note that you cannot possibly determine the line number if you don't
know what encoding the file is in (and what EOL it uses).
What you can do is count the number of bytes with the value 10 before
ude.start, like this:
lineGuess = buf[:ude.start].count(b'\n') + 1
- Philipp
On 07/25/2012 01:05 PM, jaroslav.dobrek@gmail.com wrote:
> it doesn't work
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
catch UnicodeDecodeError jaroslav.dobrek@gmail.com - 2012-07-25 04:05 -0700
Re: catch UnicodeDecodeError Andrew Berg <bahamutzero8825@gmail.com> - 2012-07-25 06:34 -0500
Re: catch UnicodeDecodeError Philipp Hagemeister <phihag@phihag.de> - 2012-07-25 13:35 +0200
Re: catch UnicodeDecodeError jaroslav.dobrek@gmail.com - 2012-07-25 05:09 -0700
Re: catch UnicodeDecodeError jaroslav.dobrek@gmail.com - 2012-07-25 05:09 -0700
Re: catch UnicodeDecodeError Dave Angel <d@davea.name> - 2012-07-25 14:50 -0400
Re: catch UnicodeDecodeError Jaroslav Dobrek <jaroslav.dobrek@gmail.com> - 2012-07-26 00:46 -0700
Re: catch UnicodeDecodeError Stefan Behnel <stefan_ml@behnel.de> - 2012-07-26 10:28 +0200
Re: catch UnicodeDecodeError Jaroslav Dobrek <jaroslav.dobrek@gmail.com> - 2012-07-26 03:51 -0700
Re: catch UnicodeDecodeError Stefan Behnel <stefan_ml@behnel.de> - 2012-07-26 13:15 +0200
Re: catch UnicodeDecodeError jaroslav.dobrek@gmail.com - 2012-07-26 04:58 -0700
Re: catch UnicodeDecodeError jaroslav.dobrek@gmail.com - 2012-07-26 04:58 -0700
Re: catch UnicodeDecodeError Philipp Hagemeister <phihag@phihag.de> - 2012-07-26 14:17 +0200
Re: catch UnicodeDecodeError Stefan Behnel <stefan_ml@behnel.de> - 2012-07-26 14:24 +0200
Re: catch UnicodeDecodeError Chris Angelico <rosuav@gmail.com> - 2012-07-26 19:46 +1000
Re: catch UnicodeDecodeError wxjmfauth@gmail.com - 2012-07-26 03:19 -0700
Re: catch UnicodeDecodeError Philipp Hagemeister <phihag@phihag.de> - 2012-07-26 14:43 +0200
csiph-web