Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #15414
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.012 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'encoded': 0.05; 'mode,': 0.07; 'option,': 0.07; 'skipping': 0.07; 'problem:': 0.09; 'am,': 0.12; 'exception': 0.12; 'subject:file': 0.13; 'cc:addr:python- list': 0.15; 'read()': 0.16; 'received:192.168.1.104': 0.16; 'subject:Python3': 0.16; 'wrote:': 0.18; 'cc:no real name:2**0': 0.21; 'header:In-Reply-To:1': 0.23; 'byte': 0.24; 'cc:2**0': 0.25; '(like': 0.25; 'not.': 0.27; 'invalid': 0.28; 'explicitly': 0.28; 'work:': 0.28; 'looks': 0.28; 'cc:addr:python.org': 0.29; 'lines': 0.30; "can't": 0.32; "isn't": 0.32; 'it.': 0.33; 'there': 0.33; 'header:User-Agent:1': 0.33; 'file.': 0.34; 'try:': 0.34; 'yet,': 0.34; 'file': 0.36; 'something': 0.36; '...': 0.36; 'but': 0.37; 'using': 0.37; 'open': 0.38; 'subject:with': 0.38; 'hello,': 0.38; 'some': 0.38; 'perhaps': 0.38; 'received:192': 0.38; 'except': 0.39; 'got': 0.39; 'subject:: ': 0.39; 'files': 0.40; 'your': 0.61; '"for': 0.67; 'header:Reply-To:1': 0.70; 'reply-to:no real name:2**0': 0.71; 'encodings.': 0.84; 'so:': 0.84; 'subject:mixed': 0.84 |
| Date | Mon, 07 Nov 2011 09:33:33 -0500 |
| From | Dave Angel <d@davea.name> |
| User-Agent | Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.23) Gecko/20110922 Thunderbird/3.1.15 |
| MIME-Version | 1.0 |
| To | Jaroslav Dobrek <jaroslav.dobrek@gmail.com> |
| Subject | Re: read from file with mixed encodings in Python3 |
| References | <406195f9-d51f-416a-b32c-e9ab07e219c7@n13g2000vbv.googlegroups.com> |
| In-Reply-To | <406195f9-d51f-416a-b32c-e9ab07e219c7@n13g2000vbv.googlegroups.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Provags-ID | V02:K0:nHoA4TtdqzvMj6EOu+/LopbCR1CtUGFAHdfZXIpSGxp +AGtMCR5sXYh1Av8qnlMyzZj9G7MH181eVzpPb0raHvai3ijfp pZoufJeRP4VAisEDgzU5JuMSlfS4hytfq1ELarcF3py+U/fnVu TLxPBpPiKc4m3ek5YJOCK+gm28dOC57JzcSVL8SrppMEYDOTGG SLvibELkMK7VgM63f997HCA6arNT9UkxL2JHTgC60aTt9iHSz6 EbQ49VMvxe3NoqFJa8LyxLDEzWuVi1eW68XKaDvNorah7NbjjJ zyAAkvEouHXIqO3WpB2DFczakkVb1Uv80/M2PIaSR3E1bevAQ= = |
| Cc | python-list@python.org |
| 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.2496.1320676445.27778.python-list@python.org> (permalink) |
| Lines | 54 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1320676445 news.xs4all.nl 6920 [2001:888:2000:d::a6]:39281 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:15414 |
Show key headers only | View raw
On 11/07/2011 09:23 AM, 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. > > Jaroslav A file with mixed encodings isn't a text file. So open it with 'rb' mode, and use read() on it. Find your own line-endings, since a given '\n' byte may or may not be a line-ending. Once you've got something that looks like a line, explicitly decode it using utf-8. Some invalid lines will give an exception and some will not. But perhaps you've got some other gimmick to tell the encoding for each line. -- DaveA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
read from file with mixed encodings in Python3 Jaroslav Dobrek <jaroslav.dobrek@gmail.com> - 2011-11-07 06:23 -0800 Re: read from file with mixed encodings in Python3 Dave Angel <d@davea.name> - 2011-11-07 09:33 -0500 Re: read from file with mixed encodings in Python3 Peter Otten <__peter__@web.de> - 2011-11-07 15:42 +0100
csiph-web