Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import string
- fhand = open("Hemingway.txt")
- for fline in fhand:
- fline = fline.rstrip()
- print(fline.translate(string.punctuation))
- import string
- # Thanks to Martijn Pieters for this improved version
- # This uses the 3-argument version of str.maketrans
- # with arguments (x, y, z) where 'x' and 'y'
- # must be equal-length strings and characters in 'x'
- # are replaced by characters in 'y'. 'z'
- # is a string (string.punctuation here)
- # where each character in the string is mapped
- # to None
- translator = str.maketrans('', '', string.punctuation)
- # This is an alternative that creates a dictionary mapping
- # of every character from string.punctuation to None (this will
- # also work)
- #translator = str.maketrans(dict.fromkeys(string.punctuation))
- s = 'string with "punctuation" inside of it! Does this work? I hope so.'
- # pass the translator to the string's translate method.
- print(s.translate(translator))
- string with punctuation inside of it Does this work I hope so
- import re
- fline = re.sub('['+string.punctuation+']', '', fline)
- import string
- #make translator object
- translator=str.maketrans('','',string.punctuation)
- string_name=string_name.translate(translator)
- for ch in string.punctuation:
- s = s.replace(ch, "'")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement