google-colaboratory - 如何使这个大小写敏感?
问题描述
我不确定如何使结果显示单词是大写还是小写。当我更改字母时,我意识到输出不再相同。
filename = 'Pride_and_Prejudice_Chapter 1.txt'
def count_common_words(filename, word):
try:
with open(filename) as f:
contents = f.read()
except FileNotFoundError:
pass
else:
word_count = contents.lower().count(word)
msg = f"'{word}' appears in {filename} about {word_count} times."
print(msg)
count_common_words(filename, 'the')
count_common_words(filename, 'you')
count_common_words(filename, 'i')
解决方案
推荐阅读
- android - 如何使用 android 使用 dropbox api v2 仅获取 .txt 文件的内容
- javascript - 使用带有 Laravel Mix 的 npm 模块,没有 .js 扩展名的“import”语句出错
- ios - 如何在 collectionView(_:didSelectItemAt:) 处调用 selectItemAt: 函数?
- cpanel - laravel-snappy 无法连接到实时服务器上的 wkhtmltopdf
- java - Java:为什么我可以在 anymous 类中访问没有 final 的局部变量 JPanel?
- database - MongoDB:查询其字段之一等于其子文档字段之一的文档?
- ios - 如何在 Swift 5 中添加滚动视图?
- python - 如果指定了标志,则在运行时完全删除特定的代码执行
- c++ - Node js服务器和c ++客户端套接字io连接,无法发出或读取数据
- java - 两个单向映射与 Hibernate 中的双向映射有何不同?