rodrigosantosbr

[Py] Count words frequency in a txt from a url

Feb 16th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.34 KB | None | 0 0
  1. # https://repl.it/languages/python3
  2.  
  3. import urllib.request
  4. import urllib.parse
  5. import urllib.error
  6.  
  7. fhand = urllib.request.urlopen('http://data.pr4e.org/romeo.txt')
  8. counts = dict()
  9. for line in fhand:
  10.   print(line.decode().strip())
  11.   words = line.decode().split()
  12.   for word in words:
  13.     counts[word] = counts.get(word, 0) + 1
  14. print(counts)
Advertisement
Add Comment
Please, Sign In to add comment