Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #52898
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <kurt.alfred.mueller@gmail.com> |
| 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; 'encoding': 0.05; 'sys': 0.07; 'welcome.': 0.07; "'no": 0.09; 'exception,': 0.09; 'expected.': 0.09; 'try:': 0.09; 'python': 0.11; 'kurt': 0.12; 'template': 0.14; '2.7.3': 0.16; '__future__': 0.16; 'easier.': 0.16; 'encoding.': 0.16; 'encodings': 0.16; 'quotes)': 0.16; 'unicode.': 0.16; 'utf8': 0.16; 'vary.': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'split': 0.19; 'thu,': 0.19; '>>>': 0.22; 'input': 0.22; 'import': 0.22; 'aug': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'skip': 0.24; 'string,': 0.24; 'unicode': 0.24; 'switch': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'mode': 0.30; '(which': 0.31; 'comments': 0.31; 'lines': 0.31; "skip:' 10": 0.31; 'usually': 0.31; '>>>>': 0.31; 'text': 0.33; 'becomes': 0.33; 'skip:# 10': 0.33; 'actual': 0.34; 'except': 0.35; 'but': 0.35; 'there': 0.35; 'list': 0.37; 'message-id:@gmail.com': 0.38; 'tasks': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'read': 0.60; 'further': 0.61; 'you.': 0.62; 'information': 0.63; 'different': 0.65; 'life': 0.66; 'here': 0.66; 'header:Reply-To:1': 0.67; 'reply- to:no real name:2**0': 0.71; 'reply-to:addr:gmail.com': 0.80; '2013': 0.98 |
| X-Virus-Scanned | amavisd-new at aerodynamics.ch |
| Date | Fri, 23 Aug 2013 17:47:46 +0200 |
| From | Kurt Mueller <kurt.alfred.mueller@gmail.com> |
| Organization | Rothenburg |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: right adjusted strings containing umlauts |
| 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> <CAPTjJmr214brLbNFyCYR3Jf7t6=CNNvj8jUqYzyaMZxC70EzWA@mail.gmail.com> |
| In-Reply-To | <CAPTjJmr214brLbNFyCYR3Jf7t6=CNNvj8jUqYzyaMZxC70EzWA@mail.gmail.com> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | 7bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| Reply-To | kurt.alfred.mueller@gmail.com |
| 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.168.1377273311.19984.python-list@python.org> (permalink) |
| Lines | 71 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1377273311 news.xs4all.nl 15917 [2001:888:2000:d::a6]:60756 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:52898 |
Show key headers only | View raw
Am 08.08.2013 18:37, schrieb Chris Angelico:
> On Thu, Aug 8, 2013 at 5:16 PM, Kurt Mueller
> <kurt.alfred.mueller@gmail.com> 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'.
>
> If you can switch to Python 3, life becomes a LOT easier. The Python 3
> input() function (which does the same job as raw_input() from Python
> 2) returns a Unicode string, meaning that it takes care of encodings
> for you.
Because I cannot switch to Python 3 for now my life is not so easy:-)
For some text manipulation tasks I need a template to split lines
from stdin into a list of strings the way shlex.split() does it.
The encoding of the input can vary.
For further processing in Python I need the list of strings to be in unicode.
Here is template.py:
##############################################################################################################
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# split lines from stdin into a list of unicode strings
# Muk 2013-08-23
# Python 2.7.3
from __future__ import print_function
import sys
import shlex
import chardet
bool_cmnt = True # shlex: skip comments
bool_posx = True # shlex: posix mode (strings in quotes)
for inpt_line in sys.stdin:
print( 'inpt_line=' + repr( inpt_line ) )
enco_type = chardet.detect( inpt_line )[ 'encoding' ] # {'encoding': 'EUC-JP', 'confidence': 0.99}
print( 'enco_type=' + repr( enco_type ) )
try:
strg_inpt = shlex.split( inpt_line, bool_cmnt, bool_posx, ) # shlex does not work on unicode
except Exception, errr: # usually 'No closing quotation'
print( "error='%s' on inpt_line='%s'" % ( errr, inpt_line.rstrip(), ), file=sys.stderr, )
continue
print( 'strg_inpt=' + repr( strg_inpt ) ) # list of strings
strg_unic = [ strg.decode( enco_type ) for strg in strg_inpt ] # decode the strings into unicode
print( 'strg_unic=' + repr( strg_unic ) ) # list of unicode strings
##############################################################################################################
$ cat <some-file> | template.py
Comments are welcome.
TIA
--
Kurt Mueller
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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