如何在python中将count()与文本文件一起使用

2024-05-29 11:02:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个非常困惑的初学者,我想知道我应该做些什么来使这项工作。我的IDE没有给我一个错误,只是没有输出

我想计算一个单词在一本书的文本文件中被找到的次数,在这种情况下,“the”就是这个单词:

with open('heidi.txt', 'r') as file:
    content = file.read()
    content.count('the') 

Tags: thetxtas错误with情况opencontent
2条回答

count()不打印任何内容。给一个变量,像这样-

with open('heidi.txt', 'r') as file:
    content = file.read()
    a = content.count('the') 
    print(a)

我学习的示例是在控制台中运行它,因此我不知道需要使用print()。谢谢你@hilberts\u酗酒问题

with open('heidi.txt', 'r') as file:
    content = file.read()
    print(content.count('the '))

相关问题 更多 >

    热门问题