从字符串中排除标点符号/非字母字符的更好方法是什么?

时间:2013-05-23 22:33:20

标签: python-2.7

我编写了这个程序来编目和枚举文本文档中的单词。如果我不必用string.translate()单独列出每一个可能的标点符号,代码就会非常整齐......是否有可能,而不是排除特定情况,只允许字母(也许是数字)字符?

from sys import argv

script_, filename = argv

bang = open(filename, 'r+')
words = bang.read()
words = words.translate(None, ',')
words = words.translate(None, '"')
words = words.translate(None, '.')
words = words.translate(None, '...')
words = words.translate(None, '?')
words = words.translate(None, '!')
words = words.translate(None, ';')
words = words.translate(None, '-')
words = words.translate(None, '\'')
words = words.translate(None, '.\'')
words = words.translate(None, '(')
words = words.translate(None, ')')
words = words.translate(None, ':')
words = str(words)
words = words.lower()
liste = words.split()
sorte = sorted(liste)

i = 0
f = 'nullooosdfgkjlkjasdihaiwuehlfkj898'
z = 1
w = 0

for wordss in sorte:
    if f == wordss:
        z += 1
        w += 1
    elif f != wordss: 
        w += 1
        print "-", z
        z = 1
        i += 1
        print "%d. %s" % (i, wordss),
        f = wordss

print "\n\n word count - %d\n" % w

3 个答案:

答案 0 :(得分:6)

  

我想在文本文档中列出单词

这个算法怎么样?在空格上拆分文本,然后去除标点符号。

>>> text = "'I wonder how many miles I've fallen by this time?' she said aloud."
>>> import string
>>> words = [x.strip(string.punctuation) for x in text.split()]
>>> words
['I', 'wonder', 'how', 'many', 'miles', "I've", 'fallen', 'by', 'this', 'time', 'she', 'said', 'aloud']

通过 don&#t; 这样的缩小词来查看此内容。因此,您可以区分我们

答案 1 :(得分:2)

您可以使用正则表达式:

import re

cleaned_text = re.sub(r'[^a-zA-Z0-9]', '', your_text)

虽然使用Counter的正则表达式会更清晰:

>>> import re
>>> from collections import Counter
>>> sentence = 'I went to the store. It was a red store.'
>>> words = re.findall(r'\w+', sentence)
>>> words
['I', 'went', 'to', 'the', 'store', 'It', 'was', 'a', 'red', 'store']
>>> Counter(words)
Counter({'store': 2, 'a': 1, 'I': 1, 'It': 1, 'to': 1, 'red': 1, 'went': 1, 'was': 1, 'the': 1})
>>> d = Counter(words)
>>> d['store']
2

答案 2 :(得分:2)

您可以使用string.punctuation,我认为这也考虑了区域设置:

import string

text = 'asdkjhqa23789ryh &*(Y(E%R W*YP@#*>WFKJHSF(P*H$ >'
cleaned_text = text
for p in string.punctuation:
    cleaned_text = cleaned_text.replace(p,'')