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 = {}
|
aliceDict = {}
|
||||||
|
outfile = open("alice_words.txt", "w")
|
||||||
|
|
||||||
for line in aliceBook:
|
for line in aliceBook:
|
||||||
words = line.split()
|
words = line.split()
|
||||||
|
|
@ -12,6 +13,7 @@ for line in aliceBook:
|
||||||
if char.isalpha():
|
if char.isalpha():
|
||||||
newWord += char
|
newWord += char
|
||||||
|
|
||||||
|
if newWord != "":
|
||||||
word = newWord
|
word = newWord
|
||||||
|
|
||||||
if word not in aliceDict:
|
if word not in aliceDict:
|
||||||
|
|
@ -19,6 +21,16 @@ for line in aliceBook:
|
||||||
else:
|
else:
|
||||||
aliceDict[word] += 1
|
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()
|
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