Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > change the first character of the line to uppercase in a text file

Reply
Thread Tools

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

 
 
powah
Guest
Posts: n/a
 
      06-26-2009
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
 
Reply With Quote
 
 
 
 
Emile van Sebille
Guest
Posts: n/a
 
      06-26-2009
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

 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      06-26-2009
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/stdty...l#file-objects

That should be enough to get you started.

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      06-26-2009
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




 
Reply With Quote
 
powah
Guest
Posts: n/a
 
      06-27-2009
On Jun 26, 4:51*pm, Chris Rebert <c...@rebertia.com> wrote:
> On Fri, Jun 26, 2009 at 12:43 PM, powah<wong_po...@yahoo.ca> 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/funct...l#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:],
 
Reply With Quote
 
Angus Rodgers
Guest
Posts: n/a
 
      06-27-2009
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
 
Reply With Quote
 
Angus Rodgers
Guest
Posts: n/a
 
      06-27-2009
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
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      06-27-2009
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


 
Reply With Quote
 
Angus Rodgers
Guest
Posts: n/a
 
      06-27-2009
On Sat, 27 Jun 2009 13:02:47 +0200, Peter Otten
<__peter__@web.de> 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
 
Reply With Quote
 
Angus Rodgers
Guest
Posts: n/a
 
      06-27-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: change the first letter into uppercase (ask) Ian Kelly Python 0 10-20-2012 11:39 PM
Re: change the first letter into uppercase (ask) Dennis Lee Bieber Python 0 10-20-2012 07:30 PM
Re: change the first letter into uppercase (ask) Zero Piraeus Python 0 10-20-2012 05:03 AM
Re: change the first letter into uppercase (ask) Dave Angel Python 0 10-20-2012 04:21 AM
Word exceptions for P:first-line { text-transform: uppercase } Luigi Donatello Asero HTML 17 10-27-2005 01:21 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57