要求:
现有一个游戏系统的日志文件,记录内容的字符串 的格式 如下所示
A girl come in, the name is Jack, level 955;
其中包含的 the name is 后面会跟着人名,随后紧跟一个逗号,其它部分可能都是会变化的,比如,可能是下面这些
A old lady come in, the name is Mary, level 94454
A pretty boy come in, the name is Patrick, level 194
请大家实现一个函数,名为getName,如下所示
-
def getName(srcStr):
-
函数体
该函数的参数srcStr 是上面所描述的格式字符串(只处理一行),该函数需要将其中的人名获取出来,并返回
比如 调用 getName('A old lady come in, the name is Mary, level 94454')
返回结果应该是 'Mary'
-
def getName(srcStr):
-
info = ('the name is ')[1].split(',')[0].strip()
-
print(info)
-
return info
-
-
srcStr = 'A girl come in, the name is Jack, level 955;'
-
getName(srcStr)
如果这是个很多行的日志,该怎么处理呢:
-
def getName(srcStr):
-
infoline=('\n') # 以换行符为切割点
-
for info in infoline:
-
if info != '' : # 判断是否为最后一行,不然会出错'list index out of range'
-
info = ('the name is ')[1].split(',')[0].strip()
-
print(info)
-
-
return
-
-
srcStr = '''
-
A girl come in, the name is Jack, level 955;
-
A old lady come in, the name is Mary, level 94454;
-
A pretty boy come in, the name is Patrick, level 194;
-
'''
-
getName(srcStr)