[英]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] 如何打印结果如下所示:1。名称(性别):数字而不是['名称','性别',数字]
I have trouble doing the string thing. 我做字符串的事情很麻烦。 It won't give me the good output.
它不会给我带来好的输出。 Any help?
有帮助吗?
Thanks 谢谢
return print(sorted(results, key=operator.itemgetter(1,2)))
isn't doing what you'd expect it to. return print(sorted(results, key=operator.itemgetter(1,2)))
没有按照你的期望去做。
Because print()
returns None
, your function will return None
. 因为
print()
返回None
,所以您的函数将return None
。 Get rid of the print statement (if you want to print the line, just put it before the return) 摆脱print语句(如果要打印该行,只需在返回之前将其放入)
Then you can do in your main()
function: 然后你可以在你的
main()
函数中做:
for person in mkEntry(file1):
print("1. {0} ({1}): {2}".format(*person))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.