Print words to file with decent formatting
This commit is contained in:
parent
8a69d379b9
commit
4111e11283
2 changed files with 2777 additions and 3 deletions
|
|
@ -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()
|
||||
|
|
|
|||
2762
thinkcspy/Chapter-12-Exercises/alice_words.txt
Normal file
2762
thinkcspy/Chapter-12-Exercises/alice_words.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue