Re: Any algorithm to preserve whitespaces?

Discussion in 'Python' started by Peter Otten, Jan 24, 2013.

  1. Peter Otten

    Peter Otten Guest

    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
    Peter Otten, Jan 24, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Santosh Kumar

    Any algorithm to preserve whitespaces?

    Santosh Kumar, Jan 19, 2013, in forum: Python
    Replies:
    0
    Views:
    134
    Santosh Kumar
    Jan 19, 2013
  2. Lie Ryan
    Replies:
    0
    Views:
    131
    Lie Ryan
    Jan 19, 2013
  3. Mitya Sirenef

    Re: Any algorithm to preserve whitespaces?

    Mitya Sirenef, Jan 19, 2013, in forum: Python
    Replies:
    0
    Views:
    135
    Mitya Sirenef
    Jan 19, 2013
  4. Santosh Kumar

    Re: Any algorithm to preserve whitespaces?

    Santosh Kumar, Jan 23, 2013, in forum: Python
    Replies:
    0
    Views:
    94
    Santosh Kumar
    Jan 23, 2013
  5. Peter Otten
    Replies:
    0
    Views:
    89
    Peter Otten
    Jan 23, 2013
Loading...

Share This Page