|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目:
根据给定的文本文件 words.txt(可将该文件存放在任意目录,注意打开文件
时要加入正确的路径)编写函数 loadWords(),words.txt 包含若干小写英文 单词。要求:
1)读入该文件,统计并输出单词的个数
2) 返回单词列表(注意:只返回单词列表即可,不要在屏幕上显示, 因为文件很大,在屏幕上显示会花很长时间)
代码:
import re
def count_words(text):
with open('D:/Desktop桌面/words.txt') as file:
text = file.read()
words = re.findall(r'[a-zA-Z]+', text) #这一步是啥意思? re模块的使用中,没找到'[a-zA-Z]+'的含义
count = len(words)
return count
print(count_words('D:/Desktop桌面'))
words.txt文件内容:
Very quietly I take my leave
As quietly as I came here;
Quietly I wave good-bye
To the rosy clouds in the western sky. The golden willows by the riverside Are young brides in the setting sun;
Their reflections on the shimmering waves Always linger in the depth of my heart.
The floating heart growing in the sludge Sways leisurely under the water;
In the gentle waves of Cambridge I would be a water plant!
That pool under the shade of elm trees
Holds not water but the rainbow from the sky; Shattered to pieces among the duckweeds
Is the sediment of a rainbow-like dream?
To seek a dream? Just to pole a boat upstream To where the green grass is more verdant;
Or to have the boat fully loaded with starlight And sing aloud in the splendor of starlight.
But I cannot sing aloud
Quietness is my farewell music;
Even summer insects heap silence for me Silent is Cambridge tonight!
您的代码目前是用于计算并输出 words.txt 文件中单词的个数。代码中的 `re.findall(r'[a-zA-Z]+', text)` 表示找出所有由大小写字母组成的单词。`[a-zA-Z]` 表示任意一个大小写字母,`+` 表示至少出现一次。所以这个正则表达式将匹配到文本中所有大小写字母组成的单词。这段代码不完全符合题目要求,需要稍作修改。这里是符合要求的代码:
- import re
- def loadWords(file_path):
- with open(file_path, 'r') as file:
- text = file.read()
- words = re.findall(r'[a-z]+', text.lower())
- print(f"单词的个数: {len(words)}")
- return words
- words_list = loadWords('D:/Desktop桌面/words.txt')
复制代码
代码中的 `re.findall(r'[a-z]+', text.lower())` 只查找小写字母的单词,并将文本全部转为小写,以满足题目要求。此外,函数名称从 `count_words` 更改为 `loadWords`,并接收文件路径作为参数,使其更符合题目要求。
|
|