Singh2013
Singh2013

Reputation: 179

Python Printing the String Result

import operator

def mkEntry(file1):
    results = []
    for line in file1:
        lst = line.rstrip().split(",")
        lst[2] = int(lst[2])
        results.append(lst)
    return print(sorted(results, key=operator.itemgetter(1,2)))


def main():
    openFile = 'names/' + 'yob' + input("Enter the Year: ") + '.txt'
    file1 = open(openFile)
    mkEntry(file1)

main()

File:

Emily,F,25021
Emma,F,21595
Madison,F,20612
Olivia,F,16100
Joaquin,M,711
Maurice,M,711
Kade,M,701
Rodrigo,M,700
Tate,M,699

How do I print out the result looks like this: 1. Name (Gender): Numbers Instead of ['name', 'gender', numbers]

I have trouble doing the string thing. It won't give me the good output. Any help?

Thanks

Upvotes: 0

Views: 101

Answers (1)

TerryA
TerryA

Reputation: 60024

return print(sorted(results, key=operator.itemgetter(1,2))) isn't doing what you'd expect it to.

Because print() returns None, your function will return None. Get rid of the print statement (if you want to print the line, just put it before the return)

Then you can do in your main() function:

for person in mkEntry(file1):
    print("1. {0} ({1}): {2}".format(*person))

Upvotes: 2

Related Questions