开发者

Split names separated with commas when surnames are also separated with commas

I have a following database field: AUX: Smith, J., Jones, M. & Ford, S. There can names from one up to 15 and others are separated by comma and last开发者_开发问答 one separated by &

How can I split this into list, where list[1]=Smith, J, list[2]=Jones, M. and so forth?


def parseNames(namestr):
    namestr   = namestr.replace('&', ',')
    nameparts = [namepart.strip() for namepart in namestr.split(',')]
    it = iter(nameparts)
    return zip(it,it)    # return list of part-pairs

def nameStr(name, fmt="{lastname}, {firstname}"):
    return fmt.format(lastname=name[0], firstname=name[1])

names = [nameStr(name) for name in parseNames("Smith, J., Jones, M. & Ford, S")]

gives you

['Smith, J.', 'Jones, M.', 'Ford, S']
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜