Try this:
with open("fin.txt") as file:
line_list = []
line_count = 0
for line in file:
letter_list = []
for letter in line:
if letter.isprintable() and not letter.isspace():
letter_list.append(letter)
if len(letter_list) != 0:
words = []
for x in range(0, len(letter_list), 16):
word = letter_list[x: x + 16]
words.append(word)
line_list.append(words)
line_count += 1
groups = [
"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"abcdefghijklmnopqrstuvwxyz",
"0123456789",
]
result = []
for la in line_list:
gr = {}
for word in la:
# word.sort()
counter = []
for group in groups:
count = 0
for n in range(len(group)):
count += word.count(group[n])
#if count != 0:
counter.append(count)
# gr["".join(word)] = " ".join(sorted(counter, reverse=True))
gr["".join(word)] = sorted(counter, reverse=True)
result.append(gr)
for value in result:
for word, score in value.items():
print(word, end=" ")
for number in score:
print(number, end=" ")
print("\t", end="")
print()
print(f"Line count = {line_count}")
The way 'count' was stored and sorted earlier would not work if it can be greater then '9', so i changed it from storing 'counts' as string to store it as list. Now it should work. Also disabled 'word.sort()' so words now should be original (not sorted).
Hope this time works