Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'url:pypi': 0.03; 'encoding': 0.05; 'utf-8': 0.07; 'expected.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'kurt': 0.12; 'encoding.': 0.16; 'errors)': 0.16; 'guess.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'utf8': 0.16; 'do,': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'header:X-Complaints- To:1': 0.27; 'usually': 0.31; 'url:python': 0.33; 'actual': 0.34; 'could': 0.34; 'but': 0.35; 'there': 0.35; 'url:org': 0.36; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'skip:. 10': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'read': 0.60; 'simple': 0.61; 'back': 0.62; 'information': 0.63; 'strategy': 0.64; 'different': 0.65 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: right adjusted strings containing umlauts Date: Thu, 08 Aug 2013 18:34:51 +0200 Organization: None References: <9781df99-f9c8-4217-aa67-7a714b7f2ebe@googlegroups.com> <5203B841.4060304@gmail.com> <5203C468.4020001@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p508494b8.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 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: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375979702 news.xs4all.nl 15990 [2001:888:2000:d::a6]:59091 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52211 Kurt Mueller wrote: > Am 08.08.2013 17:44, schrieb Peter Otten: >> Kurt Mueller wrote: >>> What do I do, when input_strings/output_list has other codings like >>> iso-8859-1? >> >> You have to know the actual encoding. With that information it's easy: >>>>> output_list >> ['\xc3\xb6', '\xc3\xbc', 'i', 's', 'f'] >>>>> encoding = "utf-8" >>>>> output_list = [s.decode(encoding) for s in output_list] >>>>> print output_list >> [u'\xf6', u'\xfc', u'i', u's', u'f'] > > How do I get to know the actual encoding? > I read from stdin. There can be different encondings. > Usually utf8 but also iso-8859-1/latin9 are to be expected. > But sys.stdin.encoding sais always 'None'. Even with $ cat funny_pic.jpg | ./mypythonscript.py you could "successfully" (i. e. no errors) decode stdin using iso-8859-1. So unfortunately you have to guess. A simple strategy is to try utf-8 and fall back to iso-8859-1 if that fails with a UnicodeDecodeError. There's also https://pypi.python.org/pypi/chardet