Return-Path: 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; 'bytes,': 0.09; 'character,': 0.09; 'input,': 0.09; 'iterate': 0.09; 'parameter': 0.09; 'skip:o 50': 0.09; 'to:name :python-dev': 0.09; 'python': 0.11; '2.7': 0.14; '314': 0.16; '315': 0.16; 'account)': 0.16; 'decode(self,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 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; 'to:name:python-list@python.org': 0.22; 'print': 0.22; 'error': 0.23; 'errors.': 0.24; 'lets': 0.24; 'unicode': 0.24; 'versions': 0.24; 'this:': 0.26; 'to:addr:python- dev': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'lines': 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; '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; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'recent': 0.39; 'sure': 0.39; 'to:addr:python.org': 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; 'to:2**2': 0.65; '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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=gv+puifU8b0odG/iVfrrLXrUeL1AlRTNUVVaXSBwYfA=; b=UDBXZQL8LFVQHaWU6f40lmbuBGJfaVu/AOlZHWTZgn0LXDuPo9JVaIfoLzVo+RWTQD iJ4blp4khWyNpnUxH3pe1+ADrpQfRMhGalU5slSJZFup0g5sJZRjN+yGZvy66xfih0Vc 7XyzhnVV4+yR0P+qt59YrlSNevdcPtk0+u0jw8geQYiWMVQfhopJBXSmjJmziHQExEKy qfJk2fN93xYj+dNwrg9L1zbPxhN11B+61fL6V/GZXYTd6OWG/2sGSe6X2qG7SoO25ysr 0LG0HzhAYTPI5eqKgv/uDkJD9YIU3hxEX8RxTIul8myjoRqkZk9H50eM2HP3hQl4lJmX TGhA== MIME-Version: 1.0 X-Received: by 10.42.64.143 with SMTP id g15mr42471706ici.59.1417378751586; Sun, 30 Nov 2014 12:19:11 -0800 (PST) In-Reply-To: References: Date: Mon, 1 Dec 2014 07:19:11 +1100 Subject: Re: [Python-Dev] Unicode decode exception From: Chris Angelico To: balaji marisetti , python-dev , "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417378761 news.xs4all.nl 2950 [2001:888:2000:d::a6]:59041 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:81739 On Sun, Nov 30, 2014 at 7:07 PM, balaji marisetti 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) > in () > 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