Can someone convert this Python 2 code into Python 3 code? **Code:** import re i
ID: 3607365 • Letter: C
Question
Can someone convert this Python 2 code into Python 3 code?
**Code:**
import re
import sys
import collections
from operator import itemgetter
def clean_words(words):
""" Removes anything but letters in word list"""
cleaned = []
for i in xrange(len(words)):
word = re.sub(r"[^a-zA-Z]", '', words[i])
if len(word) > 0:
cleaned.append(word)
return cleaned
def book_to_wordlist(filename):
""" Converts a text file to words list. """
words = []
# open book
with open(filename, 'r') as _f:
# read into buffer split into words and return
words = clean_words( _f.read().split(" ") )
return words
def print_histo(top_20_words):
""" Prints a histogram of top 20 words """
max_repeat = list( max(top_20_words, key=itemgetter(1)) )[1]
for tup in top_20_words:
print "%s " % (tup[0]),
# 20 *s for largest
for i in xrange(tup[1] * 20 / max_repeat):
sys.stdout.write("*")
def main():
books = [
"around_world.txt", "republic.txt",
"three_musketeers.txt", "walden.txt"
]
# For each of these texts perform the following tasks
for book in books:
# Create a list of 20 words most frequently appearing
words = book_to_wordlist(book)
counter = collections.Counter(words)
top20 = counter.most_common(20)
print book
# Plot frequencies of all words appearing in the text
print_histo(top20)
print
if __name__ == "__main__":
main()
Explanation / Answer
import re
import sys
import collections
from operator import itemgetter
def clean_words(words):
""" Removes anything but letters in word list"""
cleaned = []
for i in range(len(words)):
word = re.sub(r"[^a-zA-Z]", '', words[i])
if len(word) > 0:
cleaned.append(word)
return cleaned
def book_to_wordlist(filename):
""" Converts a text file to words list. """
words = []
# open book
with open(filename, 'r') as _f:
# read into buffer split into words and return
words = clean_words( _f.read().split(" ") )
return words
def print_histo(top_20_words):
""" Prints a histogram of top 20 words """
max_repeat = list( max(top_20_words, key=itemgetter(1)) )[1]
for tup in top_20_words:
print("%s " % (tup[0]))
# 20 *s for largest
for i in range(int(tup[1] * 20 / max_repeat)):
sys.stdout.write("*")
print()
def main():
books = ["around_world.txt", "republic.txt","three_musketeers.txt", "walden.txt"]
# For each of these texts perform the following tasks
for book in books:
# Create a list of 20 words most frequently appearing
words = book_to_wordlist(book)
counter = collections.Counter(words)
top20 = counter.most_common(20)
print(book)
# Plot frequencies of all words appearing in the text
print_histo(top20)
print()
if __name__ == "__main__":
main()