0

New to programming,learning python. This week are focusing on reading URL text files. Looking to read through a text file and count the amount of times the character "e" occurs.

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)

Any advice?

C.Nivs
  • 12,353
  • 2
  • 19
  • 44
  • Does this answer your question? [How to read a file line-by-line into a list?](https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list) – Nikolay Vetrov Mar 30 '20 at 19:07

1 Answers1

1

If you want to read a file on the internet, send a request to that address with the requests library. This returns you a response. response.text is the content of the web page

Try like this:

import requests

response = requests.get("https://www.gutenberg.org/files/2701/old/moby10b.txt")

print(response.text.count("e"))

Output:

116960
  • I went for this .import urllib.request fname = input('enter file') l = input('enter letter') counter = 0 content = urllib.request.urlopen(fname).read().decode('utf-8') for letter in content: if(letter==l): counter+=1 print(counter) – Evan Preston-Kelly Mar 31 '20 at 19:03