Python 面试题


`# name, age, score
tom, 12, 86

Lee, 15, 99

Lucy, 11, 58

Joseph, 19, 56`

第一栏为姓名(name),第二栏为年纪(age),第三栏为得分(score)
现在,写一个Python程序,

1)读取文件

2)打印如下结果:
得分低于60的人都有谁?
谁的名字以L开头?
所有人的总分是多少?

3)姓名的首字母需要大写,该record.txt是否符合此要求? 如何纠正错误的地方?

def loadFileTxt(fileName):
    fileContent = open(fileName)
    allLines = fileContent.readlines()
    fileContent.close()
    lines = [l[:-1].split(', ') for l in allLines if not l.startswith('#') and l.strip()]
    print([s[0] for s in lines if s[2] < '60'])
    print([s[0] for s in lines if s[0].startswith('L')])
    print([s[0].capitalize() for s in lines if s[0].islower()])
# str.strip([chars]) 去除首尾指定的字符串, 当chars为空时,去除首尾的空字符串
# l[:-1].split(', ') 先去掉没个列表元素中的\n,然后在用str.split()方法对字符串进行切片操作