3
import collections
import string
with open('cipher.txt') as f:
  f = f.read().replace(' ', '').replace('\n','').lower()
  f = f.strip(string.punctuation)

cnt = collections.Counter(f.replace(' ', ''))
for letter in sorted(cnt):
  print(letter, cnt[letter])

How do I strip the punctuation!! I can't figure out where to place that line? Can someone please modify my code to remove everything apart from letters? thank you

samir
  • 759
  • 2
  • 8
  • 13
  • 1
    The `strip` method only removes characters from the beginning and end of strings. Also, I would think it is a bad idea to open something as `f` and then reassign `f`. – rlms Sep 02 '13 at 09:49
  • 1
    @AshwiniChaudhary: Interesting, that page has no Python 3 solution. I added one. – Martijn Pieters Sep 02 '13 at 09:54

1 Answers1

8

Use str.translate() to remove codepoints; any codepoint mapping to None is removed:

remove = dict.fromkeys(map(ord, '\n ' + string.punctuation))
f.translate(remove)

The dict.fromkeys() class method makes it easy to create a dictionary mapping all keys to None.

Demo:

>>> import string
>>> remove = dict.fromkeys(map(ord, '\n ' + string.punctuation))
>>> sample = 'The quick brown fox, like, totally jumped, man!'
>>> sample.translate(remove)
'Thequickbrownfoxliketotallyjumpedman'

Adjusted to your code:

remove = dict.fromkeys(map(ord, '\n ' + string.punctuation))

with open('cipher.txt') as inputfile:
    f = inputfile.read().translate(remove)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343