![](/img/trans.png)
[英]How to count number of times a certain word occurs in json file using python?
[英]How to read through a text file in python and count the number of times a certain character occurs in it?
编程新手,学习python。 本周的重点是阅读 URL 文本文件。 寻找通读文本文件并计算字符“e”出现的次数。
import urllib.request
content = urllib.request.urlopen("https://www.gutenberg.org/files/2701/old/moby10b.txt")
content.read()
counter = 0
for 'e' in content:
counter +=1
print(counter)
有什么建议吗?
如果您想读取 Internet 上的文件,请使用 requests 库向该地址发送请求。 这会返回一个响应。 response.text 是网页的内容
像这样尝试:
import requests
response = requests.get("https://www.gutenberg.org/files/2701/old/moby10b.txt")
print(response.text.count("e"))
输出:
116960
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.