| | |
Sentence Processing
Thread Solved |
I have a file of sentences similar to this:
I would like to prosess each sentence so it only retains the capital letter in the first word, every other word in the senetnce should be lower case. The result would look like this:
Python Syntax (Toggle Plain Text)
Tom ate his Apple. Frank shoveled Snow. Henry drove a Truck.
Python Syntax (Toggle Plain Text)
Tom ate his apple. Frank shoveled snow. Henry drove a truck.
No one died when Clinton lied.
so pull in the lines:
[php]
f = open("sentences.txt","r")
lines = f.readlines()
f.close()
[/php]
Then sort through each line and uppercase each first letter and lowercase the rest; stick those new sentences into a new list:
[php]
lines_new = []
for line in lines:
lines_new.append(line[0].upper() + line[1:].lower())
[/php]
[php]
f = open("sentences.txt","r")
lines = f.readlines()
f.close()
[/php]
Then sort through each line and uppercase each first letter and lowercase the rest; stick those new sentences into a new list:
[php]
lines_new = []
for line in lines:
lines_new.append(line[0].upper() + line[1:].lower())
[/php]
Last edited by LaMouche; Nov 5th, 2006 at 6:21 pm.
Wow LaMouche, this is sweet Python code. I learned a lot about file reading/writing too. Here is what I have, showing it with my test file:
My actual sentence file works fine too. I think I understand your code. Each line is a sentence and line[0].upper() simply makes certain that the first letter in the sentence is capitalized. To this you concatenate with the + the remainder of the sentence converted to lower case. Nice example of string slicing!
Thanks!
Python Syntax (Toggle Plain Text)
""" file sentences.txt looks like that: Tom ate his Apple. Frank shoveled Snow. Henry drove a Truck. """ f_in = open("sentences.txt","r") lines = f_in.readlines() f_in.close() lines_new = [] for line in lines: lines_new.append(line[0].upper() + line[1:].lower()) f_out = open("sentences_fixed.txt", "w") for line in lines_new: f_out.write(line) f_out.close() """ file sentences_fixed.txt looks like that: Tom ate his apple. Frank shoveled snow. Henry drove a truck. """
Thanks!
No one died when Clinton lied.
![]() |
Similar Threads
- Code for Image Processing (C)
- Write sentence, then return # of vowels - Need help, plz. (C++)
- Wait while processing Note (Java)
- How to read in a sentence and insert in to linked list? (C++)
Other Threads in the Python Forum
- Previous Thread: Schedule program
- Next Thread: Simple Bar Graph
| Thread Tools | Search this Thread |
accessdenied apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionary dynamic edit enter examples file float format function gui homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysql mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pysimplewizard python random recursion redirect remote reverse scrolledtext session simple smtp software sprite statictext string strings syntax table tennis terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython






