Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'encoded': 0.05; 'broken.': 0.07; 'option,': 0.07; 'skipping': 0.07; 'problem:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'utf-8': 0.09; 'subject:file': 0.13; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject:Python3': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'from:addr:web.de': 0.23; '(like': 0.25; 'unknown': 0.28; 'work:': 0.28; "can't": 0.32; "isn't": 0.32; 'to:addr:python-list': 0.32; 'there': 0.33; 'header:X-Complaints- To:1': 0.33; 'file.': 0.34; 'try:': 0.34; 'yet,': 0.34; 'however,': 0.34; 'file': 0.36; 'something': 0.36; '...': 0.36; 'but': 0.37; 'think': 0.37; 'received:org': 0.37; 'subject:with': 0.38; 'hello,': 0.38; 'some': 0.38; 'except': 0.39; "there's": 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.39; 'files': 0.40; 'skip:l 20': 0.40; '"for': 0.67; 'encodings.': 0.84; 'often,': 0.84; 'so:': 0.84; 'subject:mixed': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: read from file with mixed encodings in Python3 Date: Mon, 07 Nov 2011 15:42:47 +0100 Organization: None References: <406195f9-d51f-416a-b32c-e9ab07e219c7@n13g2000vbv.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p5084b384.dip.t-dialin.net 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: , Newsgroups: comp.lang.python Message-ID: Lines: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1320676977 news.xs4all.nl 6976 [2001:888:2000:d::a6]:48892 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:15415 Jaroslav Dobrek wrote: > Hello, > > in Python3, I often have this problem: I want to do something with > every line of a file. Like Python3, I presuppose that every line is > encoded in utf-8. If this isn't the case, I would like Python3 to do > something specific (like skipping the line, writing the line to > standard error, ...) > > Like so: > > try: > .... > except UnicodeDecodeError: > ... > > Yet, there is no place for this construction. If I simply do: > > for line in f: > print(line) > > this will result in a UnicodeDecodeError if some line is not utf-8, > but I can't tell Python3 to stop: > > This will not work: > > for line in f: > try: > print(line) > except UnicodeDecodeError: > ... > > because the UnicodeDecodeError is caused in the "for line in f"-part. > > How can I catch such exceptions? > > Note that recoding the file before opening it is not an option, > because often files contain many different strings in many different > encodings. I don't see those files often, but I think they are all seriously broken. There's no way to recover the information from files with unknown mixed encodings. However, here's an approach that may sometimes work: >>> with open("tmp.txt", "rb") as f: ... for line in f: ... try: ... line = "UTF-8 " + line.decode("utf-8") ... except UnicodeDecodeError: ... line = "Latin-1 " + line.decode("latin-1") ... print(line, end="") ... UTF-8 äöü Latin-1 äöü UTF-8 äöü