记事本文本对齐(Python)

最初文本文件:
Let freedom ring from the mighty mountains of New York !
Let freedom ring from the heightening Alleghenies of Pennsylvania !
Let freedom ring from the snowcapped Rockies of Colorado !
Let freedom ring from the curvaceous slops of California !
But not only that ; let freedom ring from Stone Mountain of Georgia !
Let freedom ring from Lookout Mountain of Tennessee !
Let freedom ring from every hill and molehill of Mississippi !
From every mountainside , let freedom ring ! 

(统计单词出现次数)

似乎是英文字母的宽度不一样导致的文本无法对齐,请问该怎么解决呢?

我尝试过\t来解决但似乎无法解决问题,用:<最大长度也不行,应用空格替代字母后的\t才勉强达到对齐字母,但是无法对其数字。

def Tab(a,b):
    if len(a) < b:
        return f"{a +' '*(b-len(a)+3)}"
    else:
        return f"{a+' '*3}"

def main():
    with open(r'E:\sample\freedom.txt') as line:
        Line = line.readline()
        L = {}
        import string
        a = string.punctuation
        while Line != '':
            for m in a:
                Line = Line.replace(m,'')
            Line = Line.split()
            Line = [sin.lower() for sin in Line]
            for i in Line:
                L[i] = L.get(i,0) + 1
            Line = line.readline()
    fc = open(r'E:\sample\freedomcount.txt','w')
    fc.write('Word frequency table:\n')
    L1 = sorted(L,key=lambda x:x[0])
    L2 = [len(item) for item in L.keys()]
    a = 0
    c = 1
    for i in range(len(L1)):
        for k, v in L.items():
            if k == L1[a]:
                fc.write(f'{Tab(L1[a],max(L2))}{v}')
        a += 1
        c += 1
        if c // 4 == 1:
            fc.write('\n')
            c = 1
        else:
            fc.write('\t')
    fc.close()


main()

我用的是Python 3.8-32bit版本

没猜错的话,你记事本的字体是微软雅黑吧。微软雅黑不是等宽字体,简单举个例子,同样都是一个字符,i和a的宽度是不同的。

所以你只需要将记事本的字体设置为等宽的就行了。设置的位置在格式-字体里,推荐设置为Consolas字体。

字体:Consolas的显示

字体:微软雅黑的显示

如果问题得到解决,记得采纳一手。

按照单词最长字段进行设置,可以实现文本数字对齐,关键代码:

f.write(k+' '*(max(L2)-len(k)+1)+str(v)+'\t')。

 

t='''Let freedom ring from the mighty mountains of New York !
Let freedom ring from the heightening Alleghenies of Pennsylvania !
Let freedom ring from the snowcapped Rockies of Colorado !
Let freedom ring from the curvaceous slops of California !
But not only that ; let freedom ring from Stone Mountain of Georgia !
Let freedom ring from Lookout Mountain of Tennessee !
Let freedom ring from every hill and molehill of Mississippi !
From every mountainside , let freedom ring ! '''

import string
L = {} 
Line=t.split('\n')
for line in Line:
    line = line.split()
    line = [sin.lower() for sin in line]
    for i in line:
        if i not in string.punctuation:
            L[i] = L.get(i, 0) + 1
L1=sorted(L.items(),key=lambda x:x[1],reverse=True)
L2 = [len(item) for item in L.keys()]
d={x[0]:x[1] for x in L1}
with open('test1.txt','w',encoding='utf-8') as f:
    i=0
    for k,v in d.items():
        i+=1
        if i%4==1:
            f.write('\n')        
        f.write(k+' '*(max(L2)-len(k)+1)+str(v)+'\t\t')
#输出文本
“”“
let          8		freedom      8		ring         8		from         8		
of           7		the          4		mountain     2		every        2	
...
”“”