Any algorithm to preserve whitespaces?

Peter Otten __peter__ at web.de
Thu Jan 24 11:47:03 CET 2013


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





More information about the Python-list mailing list