Print words to file with decent formatting

This commit is contained in:
lolabird 2023-11-19 17:51:34 -07:00
commit 4111e11283
2 changed files with 2777 additions and 3 deletions

View file

@ -1,5 +1,6 @@
aliceBook = open("thinkcspy/Chapter-12-Exercises/alice_in_wonderland.txt", "r")
aliceBook = open("alice_in_wonderland.txt", "r")
aliceDict = {}
outfile = open("alice_words.txt", "w")
for line in aliceBook:
words = line.split()
@ -12,13 +13,24 @@ for line in aliceBook:
if char.isalpha():
newWord += char
word = newWord
if newWord != "":
word = newWord
if word not in aliceDict:
aliceDict[word] = 1
else:
aliceDict[word] += 1
print(aliceDict)
aliceList = list(aliceDict.items())
aliceList.sort()
offset = 30 - len("word")
spaces = " " * offset
outfile.write("WORD" + spaces + "COUNT\n" )
for item in aliceList:
offset = 30 - len(item[0])
spaces = " " * offset
outfile.write(item[0] + spaces + str(item[1]) + "\n" )
aliceBook.close()

File diff suppressed because it is too large Load diff