Advertisement
Guest User

capitalizr in bash

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