Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #26049

Re: catch UnicodeDecodeError

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <d@davea.name>
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; 'explicitly': 0.04; 'sys': 0.05; '(especially': 0.07; 'exception.': 0.07; 'lines.': 0.07; 'problem?': 0.07; 'try:': 0.07; '&quot;': 0.09; 'cc:addr:googlegroups.com': 0.09; 'counting': 0.09; 'loop.': 0.09; 'cc:addr:python-list': 0.10; 'ignore': 0.13; 'cases': 0.15; "'rb').read()": 0.16; 'decode': 0.16; 'wrote:': 0.17; 'char': 0.17; 'found,': 0.17; 'unicode': 0.17; 'variable': 0.20; 'question.': 0.20; 'import': 0.21; '(usually': 0.22; 'occurs': 0.22; 'wednesday,': 0.22; 'example': 0.23; 'specified': 0.23; 'cc:2**1': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'wrote': 0.26; 'am,': 0.27; "doesn't": 0.28; 'fine': 0.28; 'convert': 0.29; 'skip:& 10': 0.29; 'error': 0.30; 'point': 0.31; 'file': 0.32; "skip:' 20": 0.32; 'like:': 0.33; 'problem': 0.33; "can't": 0.34; 'text.': 0.35; 'something': 0.35; 'there': 0.35; 'except': 0.36; 'but': 0.36; 'cc:no real name:2**1': 0.36; 'thank': 0.36; 'bad': 0.37; 'quite': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'some': 0.38; 'shows': 0.38; 'received:192': 0.39; 'called': 0.39; 'received:192.168': 0.40; 'think': 0.40; 'your': 0.60; 'july': 0.60; 'skip:u 10': 0.60; 'most': 0.61; 'you.': 0.61; 'provide': 0.62; 'email addr:gmail.com': 0.63; 'ago.': 0.66; 'header:Reply- To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'yourself,': 0.75; 'trial': 0.81; 'faster.': 0.91
Date Wed, 25 Jul 2012 14:50:18 -0400
From Dave Angel <d@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0
MIME-Version 1.0
To jaroslav.dobrek@gmail.com
Subject Re: catch UnicodeDecodeError
References <04f7ff8d-9881-4a04-ab2e-b5573b5f3cd1@googlegroups.com> <mailman.2570.1343216119.4697.python-list@python.org> <b8723e64-12fa-4e53-8914-8f2b8e9c0f1d@googlegroups.com>
In-Reply-To <b8723e64-12fa-4e53-8914-8f2b8e9c0f1d@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:cTGZnCi9gwAysG2/2aTr3Bp+sUquGQEXt1/rsdal4q4 RtjA0srHXFxPs7uZZuNKWn1/1L0jjlCv29P+NKfxYV0pTdc/RC mHf8WvVxurPabQ9sIbC2aInmX6o9LmY49YeJFUVipp0uKfZ4eC 0IwydQFWxu9Apu5MKcMkfyhu8ZRaI9UIIHfzAZnimNDJYM7W4V 3edEUNoRIM119vtf8u6QeiEf/ikXigeWkY0OV7AF6cbVLDkmqj 9W3i6CZ0HIny/hzGThV3Onm5bgw6P6YAgGmi3LCGN5iuYMxQp7 oV4Cwj8BrBZRbSOOCAvW8iJah1Oh/gL4xFdxpaG2kcgJ09Xhg= =
Cc python-list@python.org, comp.lang.python@googlegroups.com
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To d@davea.name
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2581.1343242258.4697.python-list@python.org> (permalink)
Lines 45
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1343242258 news.xs4all.nl 6861 [2001:888:2000:d::a6]:34546
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:26049

Show key headers only | View raw


On 07/25/2012 08:09 AM, jaroslav.dobrek@gmail.com wrote:
> On Wednesday, July 25, 2012 1:35:09 PM UTC+2, Philipp Hagemeister wrote:
>> 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(&#39;tmp&#39;, &#39;wb&#39;).write(b&#39;\xff\xff&#39;)
>> try:
>>     buf = open(&#39;tmp&#39;, &#39;rb&#39;).read()
>>     buf.decode(&#39;utf-8&#39;)
>> except UnicodeDecodeError as ude:
>>     sys.exit(&quot;Found a bad char in file &quot; + &quot;tmp&quot;)
>>
> Thank you. I got it. What I need to do is explicitly decode text.
>
> But I think trial and error with moving files around will in most cases be faster. Usually, such a problem occurs with some (usually complex) program that I wrote quite a long time ago. I don't like editing old and complex programs that work under all normal circumstances.
>
> What I am missing (especially for Python3) is something like:
>
> try:
>     for line in sys.stdin:
> except UnicodeDecodeError:
>     sys.exit("Encoding problem in line " + str(line_number))
>
> I got the point that there is no such thing as encoding-independent lines. But if no line ending can be found, then the file simply has one single line. 

i can't understand your question.  if the problem is that the system
doesn't magically produce a variable called line_number, then generate
it yourself, by counting
in the loop.

Don't forget that you can tell the unicode decoder to ignore bad
characters, or to convert them to a specified placeholder.



-- 

DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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