Python 从文本文件中删除标点符号,包括';s和逗号

Python 从文本文件中删除标点符号,包括';s和逗号,python,replace,strip,Python,Replace,Strip,我正在创建一个文本文件中最常用单词的列表。但是我不断地得到单词和它们的所有格形式。像iphone和iphone的。在我的结果中,我还需要去掉像iphone和iphone这样的单词后面的逗号。我想把这些词作为一个整体来计算。 这是我的全部代码 # Prompuser for text file to analyze filename = input("Enter a valid text file to analyze: ") # Open file and read it line by li

我正在创建一个文本文件中最常用单词的列表。但是我不断地得到单词和它们的所有格形式。像iphone和iphone的。在我的结果中,我还需要去掉像iphone和iphone这样的单词后面的逗号。我想把这些词作为一个整体来计算。 这是我的全部代码

# Prompuser for text file to analyze
filename = input("Enter a valid text file to analyze: ")

# Open file and read it line by line
s = open(filename, 'r').read().lower()

# List of stop words that are not inportant to the meaning of the content
# These words will not be counted in the number of characters, or words.
stopwords = ['a', 'about', 'above', 'across', 'after', 'afterwards']
stopwords += ['again', 'against', 'all', 'almost', 'alone', 'along']
stopwords += ['already', 'also', 'although', 'always', 'am', 'among']
stopwords += ['amongst', 'amoungst', 'amount', 'an', 'and', 'another']
stopwords += ['any', 'anyhow', 'anyone', 'anything', 'anyway', 'anywhere']
stopwords += ['are', 'around', 'as', 'at', 'back', 'be', 'became']
stopwords += ['because', 'become', 'becomes', 'becoming', 'been']
stopwords += ['before', 'beforehand', 'behind', 'being', 'below']
stopwords += ['beside', 'besides', 'between', 'beyond', 'bill', 'both']
stopwords += ['bottom', 'but', 'by', 'call', 'can', 'cannot', 'cant']
stopwords += ['co', 'computer', 'con', 'could', 'couldnt', 'cry', 'de']
stopwords += ['describe', 'detail', 'did', 'do', 'done', 'down', 'due']
stopwords += ['during', 'each', 'eg', 'eight', 'either', 'eleven', 'else']
stopwords += ['elsewhere', 'empty', 'enough', 'etc', 'even', 'ever']
stopwords += ['every', 'everyone', 'everything', 'everywhere', 'except']
stopwords += ['few', 'fifteen', 'fifty', 'fill', 'find', 'fire']
stopwords += ['five', 'for', 'former', 'formerly', 'forty', 'found']
stopwords += ['four', 'from', 'front', 'full', 'further', 'get', 'give']
stopwords += ['go', 'had', 'has', 'hasnt', 'have', 'he', 'hence', 'her']
stopwords += ['here', 'hereafter', 'hereby', 'herein', 'hereupon', 'hers']
stopwords += ['herself', 'him', 'himself', 'his', 'how', 'however']
stopwords += ['hundred', 'i', 'ie', 'if', 'in', 'inc', 'indeed']
stopwords += ['interest', 'into', 'is', 'it', 'its', 'itself', 'keep']
stopwords += ['last', 'latter', 'latterly', 'least', 'less', 'ltd', 'made']
stopwords += ['many', 'may', 'me', 'meanwhile', 'might', 'mill', 'mine']
stopwords += ['more', 'moreover', 'most', 'mostly', 'move', 'much']
stopwords += ['must', 'my', 'myself', 'name', 'namely', 'neither', 'never']
stopwords += ['nevertheless', 'next', 'nine', 'no', 'nobody', 'none']
stopwords += ['noone', 'nor', 'not', 'nothing', 'now', 'nowhere', 'of']
stopwords += ['off', 'often', 'on','once', 'one', 'only', 'onto', 'or']
stopwords += ['other', 'others', 'otherwise', 'our', 'ours', 'ourselves']
stopwords += ['out', 'over', 'own', 'part', 'per', 'perhaps', 'please']
stopwords += ['put', 'rather', 're', 's', 'same', 'see', 'seem', 'seemed']
stopwords += ['seeming', 'seems', 'serious', 'several', 'she', 'should']
stopwords += ['show', 'side', 'since', 'sincere', 'six', 'sixty', 'so']
stopwords += ['some', 'somehow', 'someone', 'something', 'sometime']
stopwords += ['sometimes', 'somewhere', 'still', 'such', 'take']
stopwords += ['ten', 'than', 'that', 'the', 'their', 'them', 'themselves']
stopwords += ['then', 'thence', 'there', 'thereafter', 'thereby']
stopwords += ['therefore', 'therein', 'thereupon', 'these', 'they']
stopwords += ['thick', 'thin', 'third', 'this', 'those', 'though', 'three']
stopwords += ['three', 'through', 'throughout', 'thru', 'thus', 'to']
stopwords += ['together', 'too', 'top', 'toward', 'towards', 'twelve']
stopwords += ['twenty', 'two', 'un', 'under', 'until', 'up', 'upon']
stopwords += ['us', 'very', 'via', 'was', 'we', 'well', 'were', 'what']
stopwords += ['whatever', 'when', 'whence', 'whenever', 'where']
stopwords += ['whereafter', 'whereas', 'whereby', 'wherein', 'whereupon']
stopwords += ['wherever', 'whether', 'which', 'while', 'whither', 'who']
stopwords += ['whoever', 'whole', 'whom', 'whose', 'why', 'will', 'with']
stopwords += ['within', 'without', 'would', 'yet', 'you', 'your']
stopwords += ['yours', 'yourself', 'the', '—', 'The', '9', '5', 'just']
# count characters 
num_chars = len(s)

# count lines 
num_lines = s.count('\n')

# Split the words in the file
words = s.split()

# Create empty dictionary
d = {}

for i in d:
    i = i.replace('.','')
    i = i.replace(',','')
    i = i.replace('\'','')
    d.append(i.split())

# Add words to dictionary if they are not in it already.

for w in words:
    if w in d:

#increase the count of each word added to the dictionary by 1 each time it appears
       d[w] += 1
    else:
       d[w] = 1

# Find the sum of each word's count in the dictionary. Drop the stopwords.
num_words = sum(d[w] for w in d if w not in stopwords)

# Create a list of words and their counts from file
# in the form number of occurrence   word count   word
lst = [(d[w], w) for w in d if w not in stopwords]

# Sort the list of words by the count of the word
lst.sort()

# Sort the word list from greatest to least
lst.reverse()

# Print number of characters, number of lines rea, then number of words
# that were counted minus the stop words.
print('Your input file has characters = ' + str(num_chars))
print('Your input file has num_lines = ' + str(num_lines))
print('Your input file has num_words = ' + str(num_words))

# Print the top 30 most frequent words
print('\n The 30 most frequent words are \n')

# Start list at 1, the most most frequent word
i = 1

# Create list for the first 30 frequent words
for count, word in lst[:30]:

# Create list with the number of occurrence the word count then the word
    print('%2s.  %4s %s' % (i, count, word))
# Increase the list number by 1 after each entry
    i += 1
以下是我的一些结果

 1.    40 iphone

 2.    15 users

 3.    12 iphone’s

 4.     9 music

 5.     9 apple

 6.     8 web

 7.     7 new

任何帮助都将不胜感激。谢谢你

你需要说
for i in words:
而不是
for i in d:
在替换步骤中,您正在迭代一个空字典,因此没有任何变化。只需删除该循环,并将替换步骤移动到w的
循环的顶部,即:
循环,因此您只需通过一次即可

我会重做这一部分:

for w in words:
    w = w.replace('.','').replace(',','').replace('\'','').replace("’","")
    d[w] = d.get(w,0) + 1
正如现在一样,您还试图在添加到字典之前拆分
i
。它已经被分割了。此外,还需要字典的key:value。在那个点上给它一个0的值,这样以后你就可以在没有测试的情况下计算发生次数了


使用默认值为零的
.get()
(如果未找到
w
,则返回),而不是测试
,如上图所示。

您需要说
for i,换句话说:
not
for i in d:
在替换步骤中,您正在迭代一个空字典,因此没有任何变化。只需删除该循环,并将替换步骤移动到w的
循环的顶部,即:
循环,因此您只需通过一次即可

我会重做这一部分:

for w in words:
    w = w.replace('.','').replace(',','').replace('\'','').replace("’","")
    d[w] = d.get(w,0) + 1
正如现在一样,您还试图在添加到字典之前拆分
i
。它已经被分割了。此外,还需要字典的key:value。在那个点上给它一个0的值,这样以后你就可以在没有测试的情况下计算发生次数了


如果使用默认值为零的
.get()
(如果未找到
w
,则返回),而不是测试
,如果在d:
中使用w:
,则速度将加快数百倍(甚至数千倍)。如上所示。

只需一句话,列表可以跨多行,因此
['a',
可以在几行之后以
'Just'结尾]
如果没有所有的
+=
只放在一边,列表可以跨越多行,因此
['a',
可以在几行之后以
'Just']
结束,而不需要所有的
+=
谢谢您的帮助@但是我仍然有一个问题,那就是我的列表中有iphone和iphone。我已经按照您的建议清理了我的代码,并修复了一些单词。您列表中的撇号是“卷曲引号”。你也需要替换它,这可能需要Unicode字符串。我想我编辑了我的答案来替换撇号,但在iPhone上这么做,所以不能确定。谢谢你的帮助@但是我仍然有一个问题,那就是我的列表中有iphone和iphone。我已经按照您的建议清理了我的代码,并修复了一些单词。您列表中的撇号是“卷曲引号”。你也需要替换它,这可能需要Unicode字符串。我想我编辑了我的答案来替换撇号,但在iPhone上这么做,所以不能确定。