你一直在玩一個簡單的程序,讀取文本並確定首字母大寫的關鍵字。我遇到的問題是該程序不會刪除標點符號,我的意思是,佛羅多佛羅多。佛羅多,作爲不同的條目出現而不是相同。我嘗試使用導入字符串和周圍的標點符號,但它沒有奏效。Python - 關鍵字閱讀程序,無法刪除標點符號
下面是我的代碼和我使用的文本是從http://www.angelfire.com/rings/theroaddownloads/fotr.pdf(複製到名爲novel.txt的txt文檔)。 再次感謝
by_word = {}
with open ('novel.txt') as f:
for line in f:
for word in line.strip().split():
if word[0].isupper():
if word in by_word:
by_word[word] += 1
else:
by_word[word] = 1
by_count = []
for word in by_word:
by_count.append((by_word[word], word))
by_count.sort()
by_count.reverse()
for count, word in by_count[:100]:
print(count, word)
可能的重複[從Python中的字符串去除標點符號的最佳方式](http://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string-in-python ) – elethan
首先嚐試使用上述解決方案,但它似乎沒有與我的實現工作,我可能會做錯了。 –