Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. import string
  2. fhand = open("Hemingway.txt")
  3. for fline in fhand:
  4. fline = fline.rstrip()
  5. print(fline.translate(string.punctuation))
  6.  
  7. import string
  8.  
  9. # Thanks to Martijn Pieters for this improved version
  10.  
  11. # This uses the 3-argument version of str.maketrans
  12. # with arguments (x, y, z) where 'x' and 'y'
  13. # must be equal-length strings and characters in 'x'
  14. # are replaced by characters in 'y'. 'z'
  15. # is a string (string.punctuation here)
  16. # where each character in the string is mapped
  17. # to None
  18. translator = str.maketrans('', '', string.punctuation)
  19.  
  20. # This is an alternative that creates a dictionary mapping
  21. # of every character from string.punctuation to None (this will
  22. # also work)
  23. #translator = str.maketrans(dict.fromkeys(string.punctuation))
  24.  
  25. s = 'string with "punctuation" inside of it! Does this work? I hope so.'
  26.  
  27. # pass the translator to the string's translate method.
  28. print(s.translate(translator))
  29.  
  30. string with punctuation inside of it Does this work I hope so
  31.  
  32. import re
  33. fline = re.sub('['+string.punctuation+']', '', fline)
  34.  
  35. import string
  36. #make translator object
  37. translator=str.maketrans('','',string.punctuation)
  38. string_name=string_name.translate(translator)
  39.  
  40. for ch in string.punctuation:
  41. s = s.replace(ch, "'")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement