Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52211

Re: right adjusted strings containing umlauts

From Peter Otten <__peter__@web.de>
Subject Re: right adjusted strings containing umlauts
Date 2013-08-08 18:34 +0200
Organization None
References <mailman.352.1375972418.1251.python-list@python.org> <9781df99-f9c8-4217-aa67-7a714b7f2ebe@googlegroups.com> <5203B841.4060304@gmail.com> <ku0ecc$4ni$1@ger.gmane.org> <5203C468.4020001@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.360.1375979702.1251.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

right adjusted strings containing umlauts Kurt Mueller <kurt.alfred.mueller@gmail.com> - 2013-08-08 16:23 +0200
  Re: right adjusted strings containing umlauts Neil Cerutti <neilc@norwich.edu> - 2013-08-08 14:40 +0000
    Re: right adjusted strings containing umlauts MRAB <python@mrabarnett.plus.com> - 2013-08-08 16:19 +0100
  Re: right adjusted strings containing umlauts jfharden@gmail.com - 2013-08-08 07:43 -0700
    Re: right adjusted strings containing umlauts Kurt Mueller <kurt.alfred.mueller@gmail.com> - 2013-08-08 17:24 +0200
      Re: right adjusted strings containing umlauts Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-10 01:29 +0000
    Re: right adjusted strings containing umlauts Peter Otten <__peter__@web.de> - 2013-08-08 17:44 +0200
    Re: right adjusted strings containing umlauts Dave Angel <davea@davea.name> - 2013-08-08 15:50 +0000
    Re: right adjusted strings containing umlauts Kurt Mueller <kurt.alfred.mueller@gmail.com> - 2013-08-08 18:16 +0200
    Re: right adjusted strings containing umlauts Kurt Mueller <kurt.alfred.mueller@gmail.com> - 2013-08-08 18:27 +0200
      Re: right adjusted strings containing umlauts wxjmfauth@gmail.com - 2013-08-09 01:30 -0700
    Re: right adjusted strings containing umlauts Peter Otten <__peter__@web.de> - 2013-08-08 18:34 +0200
    Re: right adjusted strings containing umlauts Chris Angelico <rosuav@gmail.com> - 2013-08-08 17:37 +0100
    Re: right adjusted strings containing umlauts Dave Angel <davea@davea.name> - 2013-08-08 17:47 +0000
    Re: right adjusted strings containing umlauts Terry Reedy <tjreedy@udel.edu> - 2013-08-08 16:51 -0400
    Re: right adjusted strings containing umlauts Kurt Mueller <kurt.alfred.mueller@gmail.com> - 2013-08-23 17:47 +0200
    Re: right adjusted strings containing umlauts Kurt Mueller <kurt.alfred.mueller@gmail.com> - 2013-08-28 10:01 +0200
    Re: right adjusted strings containing umlauts Dave Angel <davea@davea.name> - 2013-08-28 10:23 +0000
      Re: right adjusted strings containing umlauts kurt.alfred.mueller@gmail.com - 2013-08-28 04:17 -0700

csiph-web