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


Groups > comp.lang.python > #37555 > unrolled thread

Re: Any algorithm to preserve whitespaces?

Started byPeter Otten <__peter__@web.de>
First post2013-01-24 11:47 +0100
Last post2013-01-24 11:47 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#37555 — Re: Any algorithm to preserve whitespaces?

FromPeter Otten <__peter__@web.de>
Date2013-01-24 11:47 +0100
SubjectRe: Any algorithm to preserve whitespaces?
Message-ID<mailman.955.1359024419.2939.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web