change the first character of the line to uppercase in a text file

Discussion in 'Python' started by powah, Jun 26, 2009.

  1. powah

    powah Guest

    How to change the first character of the line to uppercase in a text
    file?
    e.g.
    input is:
    abc xyz
    Bd ef
    gH ij

    output should be:
    Abc xyz
    Bd ef
    GH ij
     
    powah, Jun 26, 2009
    #1
    1. Advertising

  2. On 6/26/2009 12:43 PM powah said...
    > How to change the first character of the line to uppercase in a text
    > file?
    > e.g.
    > input is:
    > abc xyz
    > Bd ef
    > gH ij
    >
    > output should be:
    > Abc xyz
    > Bd ef
    > GH ij


    How far have you gotten?

    Emile
     
    Emile van Sebille, Jun 26, 2009
    #2
    1. Advertising

  3. powah

    Chris Rebert Guest

    Re: change the first character of the line to uppercase in a textfile

    On Fri, Jun 26, 2009 at 12:43 PM, powah<> wrote:
    > How to change the first character of the line to uppercase in a text
    > file?
    > e.g.
    > input is:
    > abc xyz
    > Bd ef
    > gH ij
    >
    > output should be:
    > Abc xyz
    > Bd ef
    > GH ij


    We're not in the business of doing homework. Some hints though:

    `s.upper()` converts the string in variable `s` to all upper case
    (e.g. "aBcD".upper() --> "ABCD")
    `for line in afile:` iterates over each line in a file object. `afile`
    is the file object and `line` gets assigned each line in turn.
    `s[x]` gets you the (x+1)-th character in the string `s` (e.g.
    "abcd"[2] --> "c")

    And here are the docs on working with files:
    http://docs.python.org/library/functions.html#open
    http://docs.python.org/library/stdtypes.html#file-objects

    That should be enough to get you started.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Jun 26, 2009
    #3
  4. powah

    Tim Chase Guest

    powah wrote:
    > How to change the first character of the line to uppercase in a text
    > file?
    > e.g.
    > input is:
    > abc xyz
    > Bd ef
    > gH ij
    >
    > output should be:
    > Abc xyz
    > Bd ef
    > GH ij


    While you're asking the Python list, I'd just use GNU sed:

    sed -i 's/./\U&/' myfile.txt

    I don't know if the "\U"=="make the replacement uppercase" is a
    GNU-sed specific thing, but it works here on my Debian box's
    version 4.1.5 when I tested it.

    -tkc
     
    Tim Chase, Jun 26, 2009
    #4
  5. powah

    powah Guest

    Re: change the first character of the line to uppercase in a textfile

    On Jun 26, 4:51 pm, Chris Rebert <> wrote:
    > On Fri, Jun 26, 2009 at 12:43 PM, powah<> wrote:
    > > How to change the first character of the line to uppercase in a text
    > > file?
    > > e.g.
    > > input is:
    > > abc xyz
    > > Bd ef
    > > gH ij

    >
    > > output should be:
    > > Abc xyz
    > > Bd ef
    > > GH ij

    >
    > We're not in the business of doing homework. Some hints though:
    >
    > `s.upper()` converts the string in variable `s` to all upper case
    > (e.g. "aBcD".upper() --> "ABCD")
    > `for line in afile:` iterates over each line in a file object. `afile`
    > is the file object and `line` gets assigned each line in turn.
    > `s[x]` gets you the (x+1)-th character in the string `s` (e.g.
    > "abcd"[2] --> "c")
    >
    > And here are the docs on working with files:http://docs.python.org/library/func...python.org/library/stdtypes.html#file-objects
    >
    > That should be enough to get you started.
    >
    > Cheers,
    > Chris
    > --http://blog.rebertia.com


    Thank you for your hint.
    This is my solution:
    f = open('test', 'r')
    for line in f:
    print line[0].upper()+line[1:],
     
    powah, Jun 27, 2009
    #5
  6. Re: change the first character of the line to uppercase in a text file

    On Fri, 26 Jun 2009 18:58:27 -0700 (PDT), powah
    <> wrote:

    >Thank you for your hint.
    >This is my solution:
    >f = open('test', 'r')
    >for line in f:
    > print line[0].upper()+line[1:],


    Will your program handle empty lines of input correctly?
    --
    Angus Rodgers
     
    Angus Rodgers, Jun 27, 2009
    #6
  7. Re: change the first character of the line to uppercase in a text file

    On Sat, 27 Jun 2009 11:39:28 +0100, I asked rhetorically:

    >On Fri, 26 Jun 2009 18:58:27 -0700 (PDT), powah
    ><> wrote:
    >
    >>Thank you for your hint.
    >>This is my solution:
    >>f = open('test', 'r')
    >>for line in f:
    >> print line[0].upper()+line[1:],

    >
    >Will your program handle empty lines of input correctly?


    Strangely enough, it seems to do so, but why?
    --
    Angus Rodgers
     
    Angus Rodgers, Jun 27, 2009
    #7
  8. powah

    Peter Otten Guest

    Re: change the first character of the line to uppercase in a text file

    Angus Rodgers wrote:

    > On Sat, 27 Jun 2009 11:39:28 +0100, I asked rhetorically:
    >
    >>On Fri, 26 Jun 2009 18:58:27 -0700 (PDT), powah
    >><> wrote:
    >>
    >>>Thank you for your hint.
    >>>This is my solution:
    >>>f = open('test', 'r')
    >>>for line in f:
    >>> print line[0].upper()+line[1:],

    >>
    >>Will your program handle empty lines of input correctly?

    >
    > Strangely enough, it seems to do so, but why?


    Because there aren't any. When you read lines from a file there will always
    be at least the newline character. Otherwise it would indeed fail:

    >>> for line in "peter\npaul\n\nmary".splitlines():

    .... print line[0].upper() + line[1:]
    ....
    Peter
    Paul
    Traceback (most recent call last):
    File "<stdin>", line 2, in <module>
    IndexError: string index out of range
     
    Peter Otten, Jun 27, 2009
    #8
  9. Re: change the first character of the line to uppercase in a text file

    On Sat, 27 Jun 2009 13:02:47 +0200, Peter Otten
    <> wrote:

    >Angus Rodgers wrote:
    >
    >> On Sat, 27 Jun 2009 11:39:28 +0100, I asked rhetorically:
    >>
    >>>Will your program handle empty lines of input correctly?

    >>
    >> Strangely enough, it seems to do so, but why?

    >
    >Because there aren't any. When you read lines from a file there will always
    >be at least the newline character. Otherwise it would indeed fail:
    >
    >>>> for line in "peter\npaul\n\nmary".splitlines():

    >... print line[0].upper() + line[1:]
    >...
    >Peter
    >Paul
    >Traceback (most recent call last):
    > File "<stdin>", line 2, in <module>
    >IndexError: string index out of range


    Hmm ... the \r\n sequence at the end of a Win/DOS file seems to be
    treated as a single character.

    --
    Angus Rodgers
     
    Angus Rodgers, Jun 27, 2009
    #9
  10. Re: change the first character of the line to uppercase in a text file

    On Sat, 27 Jun 2009 12:13:57 +0100, I wrote:

    >the \r\n sequence at the end of a Win/DOS file


    Of course, I meant the end of a line of text, not the end of
    the file.

    (I promise I'll try to learn to proofread my posts. This is
    getting embarrassing!)
    --
    Angus Rodgers
     
    Angus Rodgers, Jun 27, 2009
    #10
  11. Re: change the first character of the line to uppercase in a text file

    On Sat, 27 Jun 2009 12:13:57 +0100, I wrote:

    >Hmm ... the \r\n sequence at the end of a Win/DOS file seems to be
    >treated as a single character.


    For instance, if test001A.txt is this file:

    abc xyz
    Bd ef

    gH ij

    and test001E.py is this:

    f = open('test001A.txt', 'r')
    for line in f:
    print repr(line)

    then the output from "python test001E.py > temp.txt" is this:

    'abc xyz\n'
    'Bd ef\n'
    '\n'
    'gH ij\n'

    and indeed the output from "print repr(f.read())" is this:

    'abc xyz\nBd ef\n\ngH ij\n'

    How do you actually get to see the raw bytes of a file in Windows?

    OK, this seems to work:

    f = open('test001A.txt', 'rb') # Binary mode
    print repr(f.read())

    Output:

    'abc xyz\r\nBd ef\r\n\r\ngH ij\r\n'

    Indeed, when a Windows file is opened for reading in binary mode,
    the length of an "empty" line is returned as 2. This is starting
    to make some sense to me now.

    --
    Angus Rodgers
     
    Angus Rodgers, Jun 27, 2009
    #11
  12. powah

    Peter Otten Guest

    Re: change the first character of the line to uppercase in a text file

    Angus Rodgers wrote:

    > On Sat, 27 Jun 2009 13:02:47 +0200, Peter Otten
    > <> wrote:
    >
    >>Angus Rodgers wrote:
    >>
    >>> On Sat, 27 Jun 2009 11:39:28 +0100, I asked rhetorically:
    >>>
    >>>>Will your program handle empty lines of input correctly?
    >>>
    >>> Strangely enough, it seems to do so, but why?

    >>
    >>Because there aren't any. When you read lines from a file there will
    >>always be at least the newline character. Otherwise it would indeed fail:
    >>
    >>>>> for line in "peter\npaul\n\nmary".splitlines():

    >>... print line[0].upper() + line[1:]
    >>...
    >>Peter
    >>Paul
    >>Traceback (most recent call last):
    >> File "<stdin>", line 2, in <module>
    >>IndexError: string index out of range

    >
    > Hmm ... the \r\n sequence at the end of a Win/DOS


    line

    > seems to be treated as a single character.


    Yes, but "\n"[1:] will return an empty string rather than fail.
     
    Peter Otten, Jun 27, 2009
    #12
  13. Re: change the first character of the line to uppercase in a text file

    On Sat, 27 Jun 2009 13:49:57 +0200, Peter Otten
    <> wrote:

    >Angus Rodgers wrote:
    >
    >> On Sat, 27 Jun 2009 13:02:47 +0200, Peter Otten
    >> <> wrote:
    >>
    >>>Angus Rodgers wrote:
    >>>
    >>>> On Sat, 27 Jun 2009 11:39:28 +0100, I asked rhetorically:
    >>>>
    >>>>>Will your program handle empty lines of input correctly?
    >>>>
    >>>> Strangely enough, it seems to do so, but why?
    >>>
    >>>Because there aren't any. When you read lines from a file there will
    >>>always be at least the newline character. Otherwise it would indeed fail:
    >>>
    >>>>>> for line in "peter\npaul\n\nmary".splitlines():
    >>>... print line[0].upper() + line[1:]
    >>>...
    >>>Peter
    >>>Paul
    >>>Traceback (most recent call last):
    >>> File "<stdin>", line 2, in <module>
    >>>IndexError: string index out of range

    >>
    >> Hmm ... the \r\n sequence at the end of a Win/DOS

    >
    >line
    >
    >> seems to be treated as a single character.

    >
    >Yes, but "\n"[1:] will return an empty string rather than fail.


    Yes, I understood that, and it's logical, but what was worrying me
    was how to understand the cross-platform behaviour of Python with
    regard to the different representation of text files in Windows
    and Unix-like OSs. (I remember getting all in a tizzy about this
    the last time I tried to do any programming. That was in C++,
    about eight years ago. Since then, I've only written a couple of
    short BASIC programs for numerical analysis on a TI-84+ calculator,
    and I feel as if I don't understand ANYTHING any more, but I expect
    it'll come back to me. Sorry about my recent flurry of confused
    posts! If I have any silly questions of my own, I'll post then to
    the Tutor list, but in this instance, I imagined I knew what I was
    talking about, and didn't expect to get into difficulties ...) 8-P

    --
    Angus Rodgers
     
    Angus Rodgers, Jun 27, 2009
    #13
  14. powah

    Peter Otten Guest

    Re: change the first character of the line to uppercase in a text file

    Angus Rodgers wrote:

    > Yes, I understood that, and it's logical, but what was worrying me
    > was how to understand the cross-platform behaviour of Python with
    > regard to the different representation of text files in Windows
    > and Unix-like OSs. (I remember getting all in a tizzy about this


    If you are concerned about line endings open the file in universal newline
    mode:

    f = open(filename, "rU")

    """
    In addition to the standard fopen values mode may be 'U' or 'rU'. Python is
    usually built with universal newline support; supplying 'U' opens the file
    as a text file, but lines may be terminated by any of the following: the
    Unix end-of-line convention '\n', the Macintosh convention '\r', or the
    Windows convention '\r\n'. All of these external representations are seen as
    '\n' by the Python program. If Python is built without universal newline
    support a mode with 'U' is the same as normal text mode. Note that file
    objects so opened also have an attribute called newlines which has a value
    of None (if no newlines have yet been seen), '\n', '\r', '\r\n', or a tuple
    containing all the newline types seen.
    """
     
    Peter Otten, Jun 27, 2009
    #14
  15. On Sat, 27 Jun 2009 11:54:43 +0100
    Angus Rodgers <> wrote:
    > On Sat, 27 Jun 2009 11:39:28 +0100, I asked rhetorically:
    > >>f = open('test', 'r')
    > >>for line in f:
    > >> print line[0].upper()+line[1:],

    > >
    > >Will your program handle empty lines of input correctly?

    >
    > Strangely enough, it seems to do so, but why?


    The clue is the comma at the end of the print statement. It is there
    because no lines are empty. They have at least a newline.

    --
    D'Arcy J.M. Cain <> | Democracy is three wolves
    http://www.druid.net/darcy/ | and a sheep voting on
    +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
     
    D'Arcy J.M. Cain, Jun 27, 2009
    #15
  16. powah

    Terry Reedy Guest

    Peter Otten wrote:

    >>> Will your program handle empty lines of input correctly?

    >> Strangely enough, it seems to do so, but why?

    >
    > Because there aren't any. When you read lines from a file there will always
    > be at least the newline character. Otherwise it would indeed fail:


    Except possibly for the last line.
     
    Terry Reedy, Jun 27, 2009
    #16
  17. >>>>> Terry Reedy <> (TR) wrote:

    >TR> Peter Otten wrote:
    >>>>> Will your program handle empty lines of input correctly?
    >>>> Strangely enough, it seems to do so, but why?
    >>>
    >>> Because there aren't any. When you read lines from a file there will
    >>> always be at least the newline character. Otherwise it would indeed fail:


    >TR> Except possibly for the last line.


    But then that line wouldn't be empty either.

    If there is an empty line not terminated by a newline after the last
    newline, then that is called 'end-of-file' :=)
    --
    Piet van Oostrum <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
     
    Piet van Oostrum, Jun 27, 2009
    #17
  18. powah

    Peter Otten Guest

    Terry Reedy wrote:

    > Peter Otten wrote:
    >
    >>>> Will your program handle empty lines of input correctly?
    >>> Strangely enough, it seems to do so, but why?

    >>
    >> Because there aren't any. When you read lines from a file there will
    >> always be at least the newline character. Otherwise it would indeed fail:

    >
    > Except possibly for the last line.


    It may not end with a newline, but it will still contain at least one
    character.
     
    Peter Otten, Jun 27, 2009
    #18
  19. On 6/27/2009 3:39 AM Angus Rodgers said...
    > On Fri, 26 Jun 2009 18:58:27 -0700 (PDT), powah
    > <> wrote:
    >
    >> Thank you for your hint.
    >> This is my solution:
    >> f = open('test', 'r')
    >> for line in f:
    >> print line[0].upper()+line[1:],

    >
    > Will your program handle empty lines of input correctly?



    It will when the final line is changed to:

    print line[:1].upper()+line[1:]


    Emile
     
    Emile van Sebille, Jun 27, 2009
    #19
  20. powah

    MRAB Guest

    Emile van Sebille wrote:
    > On 6/27/2009 3:39 AM Angus Rodgers said...
    >> On Fri, 26 Jun 2009 18:58:27 -0700 (PDT), powah
    >> <> wrote:
    >>
    >>> Thank you for your hint.
    >>> This is my solution:
    >>> f = open('test', 'r')
    >>> for line in f:
    >>> print line[0].upper()+line[1:],

    >>
    >> Will your program handle empty lines of input correctly?

    >
    >
    > It will when the final line is changed to:
    >
    > print line[:1].upper()+line[1:]
    >

    'line' will _never_ be ''. If a line ends with a newline then that will
    be preserved returned as part of the string. This applies to the 'file'
    methods 'readline', 'readlines', etc, and the iterator, which returns a
    line. 'readline' will return '' only when it has reached the end of the
    file.
     
    MRAB, Jun 27, 2009
    #20
    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. Luigi Donatello Asero
    Replies:
    17
    Views:
    1,098
    Luigi Donatello Asero
    Oct 27, 2005
  2. Richard Schneeman
    Replies:
    16
    Views:
    496
    Daniel Bush
    Aug 27, 2008
  3. Dave Angel
    Replies:
    0
    Views:
    149
    Dave Angel
    Oct 20, 2012
  4. Zero Piraeus
    Replies:
    0
    Views:
    151
    Zero Piraeus
    Oct 20, 2012
  5. Dennis Lee Bieber

    Re: change the first letter into uppercase (ask)

    Dennis Lee Bieber, Oct 20, 2012, in forum: Python
    Replies:
    0
    Views:
    153
    Dennis Lee Bieber
    Oct 20, 2012
Loading...

Share This Page