请使用python技术统计文件A.txt中出现的词的个数(字数>2),并把结果保存到文件B.txt
时间: 2023-06-04 13:08:29 浏览: 114
Sure, 我可以使用Python技术完成这个任务。以下是代码:
```python
# 打开文件A.txt,并且读取文件内容。
with open('A.txt', 'r') as file:
content = file.read()
# 把文件内容转换成小写,用空格分隔。
words = content.lower().split()
# 移除单个字符、数字和标点符号
words = [word for word in words if len(word) > 2 and not word.isdigit() and not any(char.isdigit() or char in ".,;:'\"?!()[]{}|-_/\\") for char in word]
# 统计每个单词出现的次数
word_count = {}
for word in words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
# 打开文件B.txt并保存结果
with open('B.txt', 'w') as file:
for word, count in word_count.items():
file.write(word + ': ' + str(count) + '\n')
```
代码会打开A.txt文件,读取其中的内容,然后把内容转换成小写,用空格分隔。接下来,移除单个字符、数字和标点符号(我们只算字母数量大于2的单词),然后统计单词出现的次数,并且把结果存储到一个字典中。最后,打开B.txt文件,并把字典中的结果写入到文件中。
请注意,代码中的一些特殊字符,因此请确保在使用代码的时候进行复制。