Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 15:29:19 ~/projects/capitalizr.py$ cat ./argpa.py
- 1 #!/usr/bin/env python
- 2 #-*- coding:utf-8 -*-
- 3
- 4 # filename: capitalizer.py
- 5
- 6 # date created: 21-08-2012 19:19:36 IST
- 7 # purpose: to capitalize words in a txt file, words which has less than
- 8 # 3 words are escaped.
- 9 # author : Santosh Kumar <https://twitter.com/sntshk>
- 10
- 11 from string import punctuation
- 12 from sys import argv, stdin
- 13 from argparse import ArgumentParser, FileType
- 14
- 15 parser = ArgumentParser(
- 16 prog='capitalizr',
- 17 description='capitalize words in a file',
- 18 epilog='see project page: http://git.io/bRL0AA for more info'
- 19 )
- 20
- 21 parser.add_argument('infile', type=FileType('r'), nargs='?', default=stdin)
- 22
- 23 args = parser.parse_args()
- 24
- 25 for line in args.infile:
- 26 # Clear the extra line after each line, see issue #1 and #3
- 27 line = line.rstrip('\n')
- 28 # Split words by spaces.
- 29 words = line.split(' ')
- 30 for i, word in enumerate(words):
- 31 if len(word.strip(punctuation)) > 3:
- 32 # Capitalise and replace words longer than 3 letters (counts
- 33 # without punctuation).
- 34 if word != word.upper():
- 35 words[i] = word.capitalize()
- 36 # Print the capitalised words with spaces.
- 37 print(' '.join(words))
- 15:29:21 ~/projects/capitalizr.py$ cat testfile.txt
- 1 this line will be capitalized..
- 2 I have some punctuation in it, can't you see!?
- 3 This line has many spaces.
- 4 This line has tabs.
- 5 this this that meta santosh.
- 6 that this
- 7 fooo
- 15:31:09 ~/projects/capitalizr.py$ ./argpa.py testfile.txt
- This Line Will be Capitalized..
- I Have Some Punctuation in it, Can't you see!?
- This Line has Many Spaces.
- This Line has tabs.
- This This That Meta Santosh.
- That This
- Fooo
- 15:31:12 ~/projects/capitalizr.py$ cat testfile.txt | ./argpa.py
- 1 this Line Will be Capitalized..
- 2 I Have Some Punctuation in it, Can't you see!?
- 3 this Line has Many Spaces.
- 4 this Line has tabs.
- 5 this This That Meta Santosh.
- 6 that This
- 7 fooo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement