Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!newsfeed.tele2net.at!newsfeed.utanet.at!feeder3.cambriumusenet.nl!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.84.MISMATCH!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 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; '"': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'cases': 0.15; "'rb').read()": 0.16; 'decode': 0.16; 'wrote:': 0.17; 'char': 0.17; 'found,': 0.17; 'import': 0.21; '(usually': 0.22; 'occurs': 0.22; 'wednesday,': 0.22; 'example': 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; 'cc:addr:gmail.com': 0.27; 'fine': 0.28; 'skip:& 10': 0.29; 'error': 0.30; 'point': 0.31; 'file': 0.32; "skip:' 20": 0.32; 'like:': 0.33; 'problem': 0.33; 'received:google.com': 0.34; 'text.': 0.35; 'received:209.85': 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; 'received:209': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'some': 0.38; 'shows': 0.38; 'think': 0.40; 'your': 0.60; 'july': 0.60; 'from:no real name:2**0': 0.60; 'skip:u 10': 0.60; 'most': 0.61; 'you.': 0.61; 'provide': 0.62; 'ago.': 0.66; 'trial': 0.81; 'faster.': 0.91 Newsgroups: comp.lang.python Date: Wed, 25 Jul 2012 05:09:40 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2001:4ca0:4f01:1:230:5ff:fed2:39da; posting-account=74hy7woAAACQhc1ciracbLCiA9j-fl9e References: <04f7ff8d-9881-4a04-ab2e-b5573b5f3cd1@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 2001:4ca0:4f01:1:230:5ff:fed2:39da MIME-Version: 1.0 Subject: Re: catch UnicodeDecodeError From: jaroslav.dobrek@gmail.com To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org, jaroslav.dobrek@gmail.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1343218184 news.xs4all.nl 6857 [2001:888:2000:d::a6]:47767 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26039 On Wednesday, July 25, 2012 1:35:09 PM UTC+2, Philipp Hagemeister wrote: > Hi Jaroslav, >=20 > you can catch a UnicodeDecodeError just like any other exception. Can > you provide a full example program that shows your problem? >=20 > This works fine on my system: >=20 >=20 > import sys > open('tmp', 'wb').write(b'\xff\xff') > try: > buf =3D open('tmp', 'rb').read() > buf.decode('utf-8') > except UnicodeDecodeError as ude: > sys.exit("Found a bad char in file " + "tmp") >=20 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 pr= ograms 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 lin= e.=20