我有一个文本文件,我正在计算行数、字符数和单词数。如何通过使用 string.replace() 删除停用词(例如 (the, for, a))来清理数据
我现在有下面的代码。
前任。如果文本文件包含以下行:
"The only words to count are Apple and Grapes for this text"
Run Code Online (Sandbox Code Playgroud)
它应该输出:
2 Apple
2 Grapes
1 words
1 only
1 text
Run Code Online (Sandbox Code Playgroud)
并且不应该输出这样的词:
以下是我目前拥有的代码。
# Open the input file
fname = open('2013_honda_accord.txt', 'r').read()
# COUNT CHARACTERS
num_chars = len(fname)
# COUNT LINES
num_lines = fname.count('\n')
#COUNT WORDS
fname = fname.lower() # convert the text to lower first
words = fname.split()
d = {}
for w in …Run Code Online (Sandbox Code Playgroud)