开发者

`strip`ing the results of a split in python

i'm trying to do something pretty simple:

line = "name      :    bob"
k, v = line.lower().split(':')
k = k.strip()
v = v.strip()

is there a way to combine this into one line somehow? i found myself writing this over and over again when making parsers, and sometimes this inv开发者_运维技巧olves way more than just two variables.

i know i can use regexp, but this is simple enough to not really have to require it...


k, v = [x.strip() for x in line.lower().split(':')]


import 're'
k,v = re.split(r'\s*:\s*', line)
line = ':'.join((k,v))


>>> map(str.strip,line.lower().split(":"))
['name', 'bob']


":".join([k, v])
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜