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
    input is:
    abc xyz
    Bd ef
    gH ij

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

  2. On 6/26/2009 12:43 PM powah said...
    How far have you gotten?

    Emile van Sebille, Jun 26, 2009
    1. Advertisements

  3. powah

    Chris Rebert Guest

    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:

    That should be enough to get you started.

    Chris Rebert, Jun 26, 2009
  4. powah

    Tim Chase Guest

    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.

    Tim Chase, Jun 26, 2009
  5. powah

    powah Guest

    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
  6. Will your program handle empty lines of input correctly?
    Angus Rodgers, Jun 27, 2009
  7. Strangely enough, it seems to do so, but why?
    Angus Rodgers, Jun 27, 2009
  8. powah

    Peter Otten Guest

    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:
    .... print line[0].upper() + line[1:]
    Traceback (most recent call last):
    File "<stdin>", line 2, in <module>
    IndexError: string index out of range
    Peter Otten, Jun 27, 2009
  9. Hmm ... the \r\n sequence at the end of a Win/DOS file seems to be
    treated as a single character.
    Angus Rodgers, Jun 27, 2009
  10. 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, Jun 27, 2009
  11. For instance, if test001A.txt is this file:

    abc xyz
    Bd ef

    gH ij

    and is this:

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

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

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

    and indeed the output from "print repr(" 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(


    '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, Jun 27, 2009
  12. powah

    Peter Otten Guest

    Yes, but "\n"[1:] will return an empty string rather than fail.
    Peter Otten, Jun 27, 2009
  13. 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, Jun 27, 2009
  14. powah

    Peter Otten Guest

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

    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
  15. 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, Jun 27, 2009
  16. powah

    Terry Reedy Guest

    Except possibly for the last line.
    Terry Reedy, Jun 27, 2009
  17. 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, Jun 27, 2009
  18. powah

    Peter Otten Guest

    It may not end with a newline, but it will still contain at least one
    Peter Otten, Jun 27, 2009
  19. On 6/27/2009 3:39 AM Angus Rodgers said...

    It will when the final line is changed to:

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

    Emile van Sebille, Jun 27, 2009
  20. powah

    MRAB Guest

    '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
    MRAB, Jun 27, 2009
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.