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


Groups > comp.lang.python > #81742

Re: [Python-Dev] Unicode decode exception

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <balajimarisetti@gmail.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; 'python,': 0.02; 'subject:: [': 0.04; 'encoding': 0.05; 'subject:Python': 0.06; 'problem?': 0.07; 'python3': 0.07; 'bytes,': 0.09; 'character,': 0.09; 'input,': 0.09; 'iterate': 0.09; 'parameter': 0.09; 'skip:o 50': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '2.7': 0.14; '314': 0.16; '315': 0.16; 'account)': 0.16; 'decode(self,': 0.16; 'handled.': 0.16; 'last)': 0.16; 'non-ascii': 0.16; 'open()': 0.16; 'self.buffer': 0.16; 'subject:Dev': 0.16; 'subject:Unicode': 0.16; 'subject:exception': 0.16; 'exception': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'bit': 0.19; 'properly': 0.19; 'subject:] ': 0.20; 'input': 0.22; '(in': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'error': 0.23; 'errors.': 0.24; 'lets': 0.24; 'unicode': 0.24; 'versions': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'header :In-Reply-To:1': 0.27; 'point': 0.28; 'chris': 0.29; "doesn't": 0.30; 'errors': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'lines': 0.31; 'default,': 0.31; 'file': 0.32; 'probably': 0.32; 'url:python': 0.33; '(most': 0.33; 'reader': 0.33; 'actual': 0.34; 'could': 0.34; "can't": 0.35; 'december': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'ubuntu': 0.36; 'url:listinfo': 0.36; 'next': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; 'email addr:python.org': 0.37; 'nov': 0.38; 'pm,': 0.38; 'rather': 0.38; 'recent': 0.39; 'sure': 0.39; 'url:mail': 0.40; 'skip:u 10': 0.60; 'most': 0.60; 'simply': 0.61; 'here:': 0.62; "you'll": 0.62; 'such': 0.63; 'reached': 0.63; '30,': 0.65; 'email name:python-list': 0.65; 'skip:f 50': 0.65; 'to:addr:gmail.com': 0.65; 'life': 0.66; 'reply': 0.66; 'news': 0.67; 'between': 0.67; 'default': 0.69; '3.4': 0.84; 'hard.': 0.84; 'do:': 0.91; 'whereas': 0.91
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:to :cc:content-type; bh=TQI7y6WmjxRY75mbDaEsQQjGLBRVHvhGE+0agRrY62E=; b=uRtn+p65ikyJM+uifD82lPc9SYFzV/aABsbKHYoOzW+V+vSSRcAYtn4JT5+7sKIekw o5+poD6I4wChq0FiLJ4eUvyq8czmtY2S+ejbx8HUMcmQ/989uiJpG+2HsGzF106GQVfv 8Pivj81BtlB24oGV4vJN+ffLDc/+0YYoFJlDsrNXLbd1cFmcNoIA8w7Ej0j4Xou2XB5d EAvm4krALmU0ff33B09LMylou1T5KVsuc3T5tGJet6lrxnaP0XKb0cf15Pego4kEse4/ NCBsTcaMISYIwOZ4ylmWdAKDJvYIviWDSXaC4wwtHmXPAe72Ako5lxrmWPJudfa/Mce+ HlJQ==
X-Received by 10.182.94.204 with SMTP id de12mr34442422obb.82.1417407614958; Sun, 30 Nov 2014 20:20:14 -0800 (PST)
MIME-Version 1.0
In-Reply-To <CAPTjJmqbUm8g0qszy-RamDQrDQgb1O3ievY+86dRJO=9Jr5weA@mail.gmail.com>
References <CAKcrOwft6+6TBCpcLoG8Z4fcbpjcECUmNd4Wu-NhRbHDwr1V-g@mail.gmail.com> <CAPTjJmqbUm8g0qszy-RamDQrDQgb1O3ievY+86dRJO=9Jr5weA@mail.gmail.com>
From balaji marisetti <balajimarisetti@gmail.com>
Date Mon, 1 Dec 2014 09:49:44 +0530
Subject Re: [Python-Dev] Unicode decode exception
To Chris Angelico <rosuav@gmail.com>
Content-Type text/plain; charset=UTF-8
Cc "python-list@python.org" <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Newsgroups comp.lang.python
Message-ID <mailman.16445.1417407617.18130.python-list@python.org> (permalink)
Lines 73
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417407617 news.xs4all.nl 2886 [2001:888:2000:d::a6]:53822
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81742

Show key headers only | View raw


The default encoding is "UTF-8". It works if I do:

with open("filename", errors="ignore") as f:
    ....

So I think Python2, by default, ignores all errors whereas Python3 doesn't

On 1 December 2014 at 01:49, Chris Angelico <rosuav@gmail.com> wrote:
> On Sun, Nov 30, 2014 at 7:07 PM, balaji marisetti
> <balajimarisetti@gmail.com> wrote:
>> Hi,
>
> Hi. This list is for the development *of* Python, not development
> *with* Python, so I'm sending this reply also to
> python-list@python.org where it can be better handled. You'll probably
> want to subscribe here:
>
> https://mail.python.org/mailman/listinfo/python-list
>
> or alternatively, point a news reader at comp.lang.python. Let's
> continue this conversation on python-list rather than python-dev.
>
>> When I try to iterate through the lines of a
>> file("openssl-1.0.1j/crypto/bn/asm/x86_64-gcc.c"), I get a
>> UnicodeDecodeError (in python 3.4.0 on Ubuntu 14.04). But there is no
>> such error with python 2.7.6. What could be the problem?
>
> The difference between the two Python versions is that 2.7 lets you be
> a bit sloppy about Unicode vs bytes, but 3.4 requires that you keep
> them properly separate.
>
>> In [39]: with open("openssl-1.0.1j/crypto/bn/asm/x86_64-gcc.c") as f:
>>                  for line in f:
>>                      print (line)
>>
>> ---------------------------------------------------------------------------
>> UnicodeDecodeError                        Traceback (most recent call last)
>> <ipython-input-39-24a3ae32a691> in <module>()
>>       1 with open("../openssl-1.0.1j/crypto/bn/asm/x86_64-gcc.c") as f:
>> ----> 2     for line in f:
>>       3         print (line)
>>       4
>>
>> /usr/lib/python3.4/codecs.py in decode(self, input, final)
>>     311         # decode input (taking the buffer into account)
>>     312         data = self.buffer + input
>> --> 313         (result, consumed) = self._buffer_decode(data,
>> self.errors, final)
>>     314         # keep undecoded input until the next call
>>     315         self.buffer = data[consumed:]
>>
>>
>> --
>> :-)balaji
>
> Most likely, the line of input that you just reached has a non-ASCII
> character, and the default encoding is ASCII. (Though without the
> actual exception message, I can't be sure of that.) The best fix would
> be to know what the file's encoding is, and simply add that as a
> parameter to your open() call - perhaps this:
>
> with open("filename", encoding="utf-8") as f:
>
> If you use the right encoding, and the file is correctly encoded, you
> should have no errors. If you still have errors... welcome to data
> problems, life can be hard. :|
>
> ChrisA



-- 
:-)balaji

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


Thread

Re: [Python-Dev] Unicode decode exception balaji marisetti <balajimarisetti@gmail.com> - 2014-12-01 09:49 +0530

csiph-web