Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This program reads data from a sample text file
- # and prints out the number of capitalized words per
- # line in the file. We will use the sample file that
- # I generated in exercise 1.
- # In order for this program to work you need sample2CAP.txt
- # in the same directory as your program
- # open file in read-only mode
- # set line counter to 0
- line_number = 0
- # set capitalized words per line counter to 0
- cap_words = 0
- # open text file to read from
- with open ("sample2CAP.txt", "r") as f:
- # read each line
- for line in f:
- # count the line number
- line_number = line_number + 1
- for w in line.split():
- # if the word is capitalized
- if (w.istitle() == True):
- # count the number of capitalized words per line
- cap_words = cap_words + 1
- # print the number of capitalized words per line
- print "Line ", line_number, ": ", cap_words, "capitalized word(s)"
- # reset the cap_words counter back to 0 to count the next line
- cap_words = 0
- # we are all done. It is time to close the file and free up memory
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment