1

processing text files using python

Working with the 2016 baby name file (yob2016.txt) again, your code should
calculate the total number of babies whose names begin with each letter of the alphabet. Then,
your code should print out a nicely formatted table with each letter of the alphabet and the total
number of babies with names beginning with that letter.

Code

f = open("yob2016.txt")
letters_count = {}
for row in f:
    row = row.rstrip()
    data = row.split(",")
    name = data[0]
    count = data[2]
    if name[0] not in letters_count.keys():
        letters_count[name[0]] = 0
    letters_count[name[0]] += int(count)
print('{:<15} | {:<15} '.format("LETTER","COUNT"))
print("="*30)
for k in sorted(letters_count):
    print('   {:<10}   | {:<15} '.format(k,letters_count[k]))
print("="*30)

Output

keep visiting : shaadcoding

shaad

One Comment

  1. Excellent weblog right here! Additionally your site a lot up very fast!
    What web host are you the use of? Can I get your affiliate link on your host?

    I wish my web site loaded up as quickly as yours lol

Leave a Reply

Your email address will not be published. Required fields are marked *