Brit
Brit

Reputation: 23

Trouble printing things out so that they are aligned

I'm trying to print out a list of results i have but I want them to be aligned. They currently look like:

       table        
word:          frequency:
i                   9
my                   2
to                   2
test                   2
it                   2
hate                   1
stupid                   1
accounting                   1
class                   1
because                   1
its                   1
from                   1
six                   1
seven                   1
pm                   1
how                   1
is                   1
this                   1
helping                   1
becuase                   1
im                   1
going                   1
do                   1
a                   1
little                   1
on                   1
freind                   1
ii                   1

I want the frequency to be aligned with each other so they aren't going in this weird zig zag form. I tried playing around with adding things to the format but it didn't work. This is what my code looks like:

import string
from collections import OrderedDict


f=open('mariah.txt','r')
a=f.read()  # read the text file like it would normal look ie no \n or anything
# print(a)
c=a.lower()  # convert everything in the textfile to lowercase
# print(c)
y=c.translate(str.maketrans('','',string.punctuation))  # get rid of any punctuation
# print(y)
words_in_novel=y.split()  # splitting every word apart. the default for split is on white-space characters. Why when i split like " " for the spacing is it then giving me \n?

#print(words_in_novel)

count={}

for word in words_in_novel:
    #print(word)
    if word in count:  # if the word from the word_in_novel is already in count then add a one to that counter
        count[word]+=1
    else:
        count[word]=1  # if the word is the first time in count set it to 1

print(count)
print("\n\n\n\n\n\n\n")
# this orderes the dictionary where its sorts them by the second term wehre t[1] refers to the term after the colon
# reverse so we are sorting from greatest to least values
g=(sorted(count.items(), key=lambda t: t[1], reverse=True))
# g=OrderedDict(sorted(count.items(), key=lambda t: t[1]))
print(g)
print("\n\n\n\n\n\n\n")
print("{:^20}".format("table"))
print("{}{:>20}".format("word:","frequency:"))
for i in g:
    # z=g[i]
    # print(i)
    # a=len(i[0])
    # print(a)
    # c=50+a
    # print(c)
    print("{}{:>20}".format(i[0],i[1]))

Does anyone know how to make them going in a straight line?

Upvotes: 0

Views: 58

Answers (4)

Joao  Vitorino
Joao Vitorino

Reputation: 3256

Align words too

print("{:<10}{:>10}".format(i[0],i[1]))

Upvotes: 0

Wilfredo
Wilfredo

Reputation: 1548

If you even find that the frequency is greater than a single digit you could try something like this:

max_len = max(len(i[0]) for i in g)
format_str = "{{:<{}}}{{:>{}}}".format(max_len, 20 - max_len)
for i in g:
    print(format_str.format(i[0], i[1]))

Upvotes: 0

Yasin Yousif
Yasin Yousif

Reputation: 967

Ok , for the part of your code :

for i in g:
    r = " "*25
    #print("{}{:>20}".format(i[0],i[1]))
    r[:len(i[0])] = i[0]
    r = r[:22]+str(i[1])
    print(r)

it should work

Upvotes: 0

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

You need to adjust the width/alighnment of your 1st column, not the 2nd.
The right way:

...
print("{:<20}{}".format("word:","frequency:"))
for i in g:
    print("{:<20}{}".format(i[0],i[1]))

The output would look as:

word:               frequency:
i                   9
my                  2
...
accounting          2
class               1
because             1
...

Upvotes: 2

Related Questions