Notice
Recent Posts
Recent Comments
Link
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Tags
more
Archives
Today
Total
관리 메뉴

PythonHolic

[python] txt 파일 내의 영어 알파벳 갯수 새기 본문

파이썬 과제 도와주기 일지

[python] txt 파일 내의 영어 알파벳 갯수 새기

devpy 2020. 6. 25. 15:24

def main():
    #list for print
    temp  = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p'
             ,'q','r','s','t','u','v','w','x','y','z']
    count =0

    while True:
        try:
            file = input("Enter a filename : ").strip()
            file = open(file,'r')
            break
        except:
            print("파일명이 존재하지 않습니다")

    counts = []
    for i in range(26):
        counts.append(0)

    for j in file:
        countLetters(j.lower(),counts)

    while count<=25:
        for i in counts:
            print("{} : ".format(count+1),temp[count],"appears {} times".format(i))
            count+=1

def countLetters(line,counts):
    for k in line:
        if k.isalpha() == True:
           counts[ord(k)-97]+=1
    
main()
        
Comments