This question already has answers here:
Best way to strip punctuation from a string

(28 个回答)


7年前关闭。



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])

怎么去掉标点符号!!我不知道把那条线放在哪里?
有人可以修改我的代码以删除除字母之外的所有内容吗?谢谢你

最佳答案

使用 str.translate() 删除代码点;删除到 None 的任何代码点映射:

remove = dict.fromkeys(map(ord, '\n ' + string.punctuation))
f.translate(remove)
dict.fromkeys() 类方法可以轻松创建将所有键映射到 None 的字典。

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

调整到您的代码:
remove = dict.fromkeys(map(ord, '\n ' + string.punctuation))

with open('cipher.txt') as inputfile:
    f = inputfile.read().translate(remove)

关于python - 如何从文本文件中去除标点符号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18570182/

10-12 16:36