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


Groups > comp.lang.python > #37555

Re: Any algorithm to preserve whitespaces?

From Peter Otten <__peter__@web.de>
Subject Re: Any algorithm to preserve whitespaces?
Date 2013-01-24 11:47 +0100
Organization None
References (4 earlier) <CAE7MaQbgpj+xsELBEkmHq1GS9-cRNph+O-sq=5oPh4QTt=2Z=g@mail.gmail.com> <kdpmo3$pos$1@ger.gmane.org> <CAE7MaQYCieZJAJtJr8S4KSxpMF8UFSOx6XY_X59c-RmPRbHG-g@mail.gmail.com> <kdquvs$g74$1@ger.gmane.org> <CAE7MaQZ-3K95=S7tAHxEfR3StdVmHRJ1q4bewiy-Yn0rU5cLGQ@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.955.1359024419.2939.python-list@python.org> (permalink)

Show all headers | View raw


Santosh Kumar wrote:

> But I can; see: http://pastebin.com/ZGGeZ71r

You have messed with your cat command -- it adds line numbers.
Therefore the output of

cat somefile | ./argpa.py

differs from

./argpa.py somefile

Try

./argpa.py < somefile

to confirm my analysis. As to why your capitalisation algorithm fails on 
those augmented lines: the number is separated from the rest of the line by 
a TAB -- therefore the first word is "1\tthis" and the only candidate to be 
capitalised is the "1". To fix this you could use regular expressions (which 
I wanted to avoid initially):

>>> parts = re.compile("(\s+)").split(" 1\tthis is it")
>>> parts
['', ' ', '1', '\t', 'this', ' ', 'is', ' ', 'it']

Process every other part as you wish and then join all parts:

>>> parts[::2] = [s.upper() for s in parts[::2]]
>>> parts
['', ' ', '1', '\t', 'THIS', ' ', 'IS', ' ', 'IT']
>>> print "".join(parts)
 1      THIS IS IT

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


Thread

Re: Any algorithm to preserve whitespaces? Peter Otten <__peter__@web.de> - 2013-01-24 11:47 +0100

csiph-web